Collection of methods for working with arrays in ko.utils in knockout

Source: Internet
Author: User

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.arrayForEachSource:

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.arrayMapSource:

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.arrayFilterSource:

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.arrayIndexOfSource:

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.arrayRemoveItemSource:

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.arrayGetDistinctValuesSource:

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.findsimilar 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.arrayFirstSource:

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]);

arrthe last value is[1, 2, 3, 4, 5]

ko.utils.arrayPushAllSource:

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)

includedFor 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.addOrRemoveItemSource:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.