forEach
-
The method returns no value by iterating over each value in the array. Method takes two arguments, the first of which must be a callback function. The function takes three arguments: the first is the value in the traversal group, the second is the index in the traversal group, and the third is the traversal group itself. The second argument is optional, changing the this pointer inside the first argument.
var data = [ {name: 'zhangsan', age: 23}, {name: 'lisi', age: 24}, {name: 'wangwu', age: ForEach (function(item, index, arr){// forEach(function(item, index, arr){// forEach(function(item, index, arr){ console.log(item); console.log(index); console.log(arr); console.log(this); }, {name: 'zhaoliu'}); / / rewrite forEach Array. Prototype. MyForEach = function (fn) {var arr = this, / / save the len = arr. Calls to the function of Array length, arg = arguments[1] || window; Var I = 0; var I = 0; var I = 0; i < len; i++){ fn.apply(arg, [arr[i], i, arr]); }}Copy the code
filter
-
Filter an array, the same as forEach, but with a return value, returns a new array of members that meet the criteria of the callback function.
var data = [ {name: 'zhangsan', age: 23}, {name: 'lisi', age: 24}, {name: 'wangwu', age: 25} ] var res = data.filter(function(item, index, arr){ return item.age > 23; }) console.log(res); // -> [{name: 'lisi', age: 24}, {name: 'wangwu', age: MyFilter = function (fn) {var arr = this, len = arr. arg = arguments[1] || window, res = []; For (var I = 0; i < len; Fn. Apply (arg, [arr[I], I, arr])? res.push(arr[i]) : ''; } return res; // Return the array}Copy the code
-
Note: The array returned by the original filter and the original array are only lightly copied, that is, if the values in the array are reference values, modifying the returned array will also change the original array. Solution: Make a deep copy before adding our own function to the returned array.
map
-
It returns a new array whose members are the values of the original array after the callback function. The new array is also mapped to the original array (if the new array is a reference value, the original array will also change).
var data = [ {name: 'zhangsan', age: 23}, {name: 'lisi', age: 24} ] var res = data.map(function (item, index, arr) { item.name = 'wangwu'; return item; }) console.log(res, data); / / rewrite the map Array. Prototype. MyMap = function (fn) {var arr = this, len = arr. Length, arg = the arguments [1] | | window, res = []; for(var i = 0; i < len; I ++) {// Break the mapping, here you can clone res.push(fn.apply(arg, [arr[I], I, arr]) for each item of the original array; } return res; }Copy the code
every
-
Returns a Boolean value true for the condition that the array members all compound the callback function, false otherwise.
var data = [14, 18, 22]; var res = data.every(function (item, index, arr) { return item > 18; }) console.log(res); Function (fn) {var arr = this, len = arr. arg = arguments[1] || window, res = true; for(var i = 0; i < len; I++){// if one of these does not meet the criteria, we stop the loop and return false if(! fn.apply(arg, [arr[i], i, arr])){ res = false; break; } } return res; }Copy the code
some
-
Also returns a Boolean value, true if one member is satisfied, false otherwise.
var data = [14, 18, 22]; var res = data.some(function (item, index, arr) { return item > 18; }) console.log(res); MySome = function (fn) {var arr = this, len = arr. arg = arguments[1] || window, res = false; for(var i = 0; i < len; If (fn. Apply (arg, [arr[I], I, arr])){res = true; break; } } return res; }Copy the code
reduce
-
The inductive function, the method takes two arguments. The first argument is the callback function. The callback takes four arguments: previousValue (the value returned by the previous callback, or the provided initialValue), currentValue (the current member being processed), The third index (the index of the current member), and the fourth array (the array itself that calls the method); The second argument to the method initialValue (as the first argument to the first callback function). The return value is previousValue/initialValue.
var data = [ {name: 'zhangsan', age: 13}, {name: 'lisi', age: 16}, {name: 'wangwu', age: 18}, {name: 'zhaoliu', age: 22} ] var res = data.reduce(function (pre, item, index, arr) { if(item.age >= 18){ pre.push(item); } return pre; } []); console.log(res); If the third argument is passed, Array.prototype.myReduce = function (fn, initialValue) {var arr = this, len = arr. Arg = the arguments [2] | | window, / / the parameters of additional function item; for(var i = 0; i < len; i++){ item = arr[i]; initialValue = fn.apply(arg, [initialValue, item, i, arr]); } return initialValue; }Copy the code