Each set of frameworks will basically have a tool class, such as: Vue
in, Vue.util
in, Knockout
ko.utils
and jQuery
directly put some tool classes in the $
inside, if you need more tool class can try Lodash. This article only describes some of the ko.utils
ways in which arrays are handled in Knockout.
Ko.utils.arrayForEach (
Array,
Callback)
Consistent with the Array.prototype.forEach Effect. The supplied function (callback Function) is executed once for each element of the Array. How to Use:
var arr = [1, 2, 3, 4];ko.utils.arrayForEach(arr, function(el, index) { console.log(el)});
The above respectively output: 1
,, 2
3
,4
ko.utils.arrayForEach
Source:
function (array, action) { for (var i = 0, j = array.length; i < j; i++) action(array[i], i);}
Ko.utils.arrayMap (
Array,
Callback)
Consistent with the Array.prototype.map Effect. Returns a new array that consists of the return value of each element in the original array that is called after a specified method. How to Use:
var arr = [1, 2, 3, 4];var newArr = ko.utils.arrayMap(arr, function(el, index) { return el + 1;});
The above-mentioned Newarr are:[2, 3, 4, 5]
ko.utils.arrayMap
Source:
ko.utils.arrayMap = function (array, mapping) { array = array || []; var result = []; for (var i = 0, j = array.length; i < j; i++) result.push(mapping(array[i], i)); return result;}
Ko.utils.arrayFilter (
Array,
Callback)
Consistent with the Array.prototype.filter Effect. Tests all elements with the specified function and creates a new array that contains all the elements passed through the Test. How to Use:
var arr = [1, 2, 3, 4];var newArr = ko.utils.arrayFilter(arr, function(el, index) { return el > 2;});
The above newArr
is obtained as:[3, 4]
ko.utils.arrayFilter
Source:
ko.utils.arrayFilter = function (array, predicate) { array = array || []; var result = []; for (var i = 0, j = array.length; i < j; i++) if (predicate(array[i], i)) result.push(array[i]); return result;}
Ko.utils.arrayIndexOf (
Array,
Item)
Consistent with the Array.prototype.indexOf Effect. Returns the first index value found in the array for the given element, otherwise returns-1. How to Use:
var arr = [1, 2, 3, 4];var index = ko.utils.arrayIndexOf(arr, 2);
The value given above index
is1
ko.utils.arrayIndexOf
Source:
function (array, item) { if (typeof Array.prototype.indexOf == "function") return Array.prototype.indexOf.call(array, item); for (var i = 0, j = array.length; i < j; i++) if (array[i] === item) return i; return -1;}
Ko.utils.arrayRemoveItem (
Array,
Itemtoremove)
Removes an element of the specified value from the Array. How to Use:
var arr = [1, 2, 3, 4, 2];ko.utils.arrayRemoveItem(arr, 2);
Above arr
now for[1, 3, 4, 2]
ko.utils.arrayRemoveItem
Source:
function (array, itemToRemove) { var index = ko.utils.arrayIndexOf(array, itemToRemove); if (index > 0) { array.splice(index, 1); } else if (index === 0) { array.shift(); }}
Ko.utils.arrayGetDistinctValues (
Array)
The array is de-weighed (if It is very inefficient in length), and the new array is returned after the weight is Gone. How to Use:
var arr = [1, 2, 3, 4, 2, 4, ‘1‘];var newArr = ko.utils.arrayGetDistinctValues(arr);
The resulting newArr
value is[1, 2, 3, 4, ‘1‘]
ko.utils.arrayGetDistinctValues
Source:
ko.utils.arrayGetDistinctValues = function (array) { array = array || []; var result = []; for (var i = 0, j = array.length; i < j; i++) { if (ko.utils.arrayIndexOf(result, array[i]) < 0) result.push(array[i]); } return result;}
Ko.utils.arrayFirst (
Array,
Callback[,
Thisarg])
Array.prototype.find
similar to methods (the name and find deviations are too large). Returns the first element that satisfies a Condition. How to Use:
var arr = [ {name: "apple"}, {name: "banana"}, {name: "cherries"}];var item = ko.utils.arrayFirst(arr, function(el, index){ return el.name === "banana";})
ko.utils.arrayFirst
Source:
function (array, predicate, predicateOwner) { for (var i = 0, j = array.length; i < j; i++) if (predicate.call(predicateOwner, array[i], i)) return array[i]; return null;}
Ko.utils.arrayPushAll (
Array,
Valuestopush)
Merges the array valuesToPush
into the array array
. How to Use:
var arr = [1, 2, 3];ko.utils.arrayPushAll(arr, [4, 5]);
arr
the last value is[1, 2, 3, 4, 5]
ko.utils.arrayPushAll
Source:
ko.utils.arrayPushAll = function (array, valuesToPush) { if (valuesToPush instanceof Array) array.push.apply(array, valuesToPush); else for (var i = 0, j = valuesToPush.length; i < j; i++) array.push(valuesToPush[i]); return array;}
Ko.utils.addOrRemoveItem (
Array,
value,
included)
included
For true
, then array
contains no value
processing, no is added, included
for false
, then array
contains value
delete, no is not processed. How to Use:
var arr = [1, 2, 3];ko.utils.addOrRemoveItem(arr, 4, true); /// arr为[1, 2, 3, 4]// 或者ko.utils.addOrRemoveItem(arr, 4, false); /// arr为[1, 2, 3]// 或者ko.utils.addOrRemoveItem(arr, 2, true); /// arr为[1, 2, 3]// 或者ko.utils.addOrRemoveItem(arr, 2, false); /// arr为[1, 3]
ko.utils.addOrRemoveItem
Source:
function(array, value, included) { var existingEntryIndex = ko.utils.arrayIndexOf(ko.utils.peekObservable(array), value); if (existingEntryIndex < 0) { if (included) array.push(value); } else { if (!included) array.splice(existingEntryIndex, 1); }}
Ko is basically the way to deal with arrays, if you know that Ko has these methods, then in the older browser (IE8 and Below) development can make you much easier
Https://www.xiaoboy.com/topic/ko-utils-array-function.html
Collection of methods for working with arrays in ko.utils in knockout