1.map()

The map() method is defined in the JavaScript Array and returns a new processed Array

Map () does not check for empty arrays

Map () does not change the original array

1. Grammar:

Array. map(function(value, index, arr), thisIndex) function(value, index, arr) : is a function that is executed for each element in the array. Function parameter: value: mandatory. The value of the current element. Index: Optional. The index of the current element. Arr: Optional. The array object to which the current element belongs. ThisValue: Optional. Object is used when the callback is executed, passed to the function, and used as the value of "this".Copy the code

2. Example:

Array = [{id: 1, name: "select 1"}, {id: 2, name: "select 2"}, {id: 3, name: "select 3"}]; var array2 = array.map(function(item, index, arr) { return Object.assign({}, { value: item.id }); }); Var array2 = array.map((item, index) => {return item.name}); Array =[{index:0},{index:1},{index:2}]; array.map(item=>{ item.active=false; return item; })Copy the code

2.some()

Some () checks whether an element in an array satisfies a specified condition

Some () does not check for empty arrays

Some () doesn’t change the array

The some() method executes each element of the array in turn:

If one element meets the criteria, the expression returns true, and the remaining elements are not tested.

If no element satisfies the condition, false is returned.

1. Syntax: array.some(function(value, index, arr), thisIndex) function(value, index, arr) : is a function that is executed for each element in the array. Function parameter: value: mandatory. The value of the current element. Index: Optional. The index of the current element. Arr: Optional. The array object to which the current element belongs. ThisValue: Optional. Object is used when the callback is executed, passed to the function, and used as the value of "this". 2. Example:Copy the code

1. Determine which of the arrays meets the criteria

var ages = [3, 10, 18, 20]; var b = []; ages.some((item, index) => { if (item > 5) { b.push(item); }});Copy the code

2. Check whether the array contains the value

var ages = [3, 10, 18, 20]; var b = []; var s = 10; ages.some((item, index) => { if (item == s) { b.push(item); }});Copy the code

3.every()

Every () checks whether an element in an array satisfies a specified condition

Every () does not check the empty array Every () does not change the original array Every () executes each element of the array in turn: if one element fails, the expression returns false, and the rest of the elements are not checked. Returns true if all elements satisfy the condition. Var ages = [3, 10, 18, 20]; ages.every((item, index) => { if (item >10 ) { b.push(item); }});Copy the code

4.filter()

The filter() method returns a new array filter() does not check the empty array filter() does not change array 1 by checking the qualifying elements in the specified array. Var b = ages.filter((item, index) => {if (item > 10) {return item; }});Copy the code

5.find()

The find() method returns the value of the first element of the array that passed the test. Find () does not check the empty array. Find () does not change the original array. It will not be called again if there is no element that meets the criteria and returns undefined 1. Var ages = [3, 4, 5, 6]; var b = ages.find(item => { if (item > 4) { return item; }}); //b=5;Copy the code

6. Summary:

Map (),some(),every(),filter() have the same syntax and usage; 1. Map () and filter() both return a new array, map() returns the processed data, and filter() returns the qualified data. 2. Some () and every() both return Boolean values, Some () returns true as long as one of the values matches, and every() returns true as long as every value matchesCopy the code