A, the find
The find () method returns the first element in the array that meets the criteria, or undefined if it does not
let arr = [1.2.3.4.5.6.7];
let result = arr.find(function (item) {
return item > 4; })console.log(result);/ / 5
console.log(arr);/ /,2,3,4,5,6,7 [1]
Copy the code
Second, the filter
The filter() method returns a new array that filters all the elements in the array that meet the criteria, or returns an empty array if no symbol criteria are found.
let arr = [1.2.3.4.5.6.7];
let result = arr.filter(function (item) {
return item > 4; })console.log(result);/ / [5, 6]
console.log(arr);/ /,2,3,4,5,6,7 [1]
Copy the code
Third, forEach
The forEach() method is used to call each element of the array once, but it does not return a value (or undefined, even if the callback has a return).
ForEach () does not perform callbacks on empty arrays.
let arr = [1.2.3.4.5.6.7];
let sum = 0;
let result = arr.forEach(function (item) { sum += item ; })console.log(result);//undefined
console.log(arr);/ /,2,3,4,5,6,7 [1]
console.log(sum);/ / 28
Copy the code
Fourth, the map
The map() method returns an array of elements that are filtered and processed by the callback, and the result is returned in a new array (if no value is returned, each element of the new array is undefined).
let arr = [1.2.3.4.5.6.7];
let result = arr.map(function (item) {
return item*2; })console.log(result);/ / [14] 2,4,6,8,10.12.
console.log(arr);/ /,2,3,4,5,6,7 [1]
Copy the code