Dead simple utility that attaches Underscore.js array and collection functions (and mutators) to Knockout.js observable arrays.
npm install underscore-ko.\build\underscore-ko.min.js and put it in your project.
build\underscore-ko.d.ts.
js
var vm = {
arr: ko.observableArray([1, 2, 3]);
};
vm.arr.each(function (x) {
// do something
});
// This returns a raw array, not a ko.observableArray
var newArr = vm.arr.union([0, 1]);
// but this will append 0 and 1 to the underlying array and trigger a change notification
vm.arr.union_([0, 1]);
`
In addition, there are several functions you can use that will mutate (change) the underlying array, which are provided as convenient shortcuts.
`js
// Re-structure the observable array [1, 2, 3]
vm.arr.without_(2);
// vm.arr() is now equal to [1, 3]
// Without this, you would need to do:
vm.arr(vm.arr.without(2));
`
Live Demo ##
View the live jsFiddle demo
Documentation ##
See the Underscore.js documentation for more information on the API. All array and collection methods are supported with the exception of any I felt didn't provide value (.toArray() for example).
See spec.js for examples of how to use specific functions, but I'm telling you, it's as you'd expect.
$3
These methods change the underlying array instead of returning a copy of the array.
* filter_, select_
* reject_
* invoke_
* sortBy_
* groupBy_
* shuffle_
* rest_, tail_
* compact_
* flatten_
* without_
* union_
* intersection_
* difference_
* uniq_, unique_
* zip_
* unzip_`