Usage of ES6 arrays

1. forEach() is used to loop through the for

  •  Array name.forEach(function (item,index,arr) {})
  • item : each item of the array, index : the index of the array, arr : the original array
  • Function: used to traverse the array
let arr = [1, 2, 3, 4];
console.log(arr);
let arr1 = arr.forEach((item, index, arr) => {
    console.log(item, index, arr);
})

2.map mapping array

  • Array name.map(function (item,index,arr) {})
  • item : each item of the array, index : the index of the array, arr : the original array
  • Function: It will return an array with the same size as your original array, and the value of the new array depends on your return.
let arr = [1, 2, 3, 4];
console.log(arr);
let arr1 = arr.map((item, index, arr) => {
    return item * 10
})
console.log(arr1);

Result graph: 

3.filter filter array

  • Array name.filter(function (item,index,arr) {})
  • item : each item of the array, index : the index of the array, arr : the original array
  • Function: A filtered array will be returned, and the filtering rules depend on your return.

Look at the result below, which is to return the elements that satisfy your return to a new array.

let arr = [1, 2, 3, 4];
console.log(arr);
let arr1 = arr.filter((item, index, arr) => {
    return item > 2;
})
console.log(arr1);

result 

 4. every judge whether the array meets all the conditions

  • Array name.every(function (item,index,arr) {})
  • item : each item of the array, index : the index of the array, arr : the original array
  • Function: To judge whether all the elements in your array meet the conditions, the return value is a Boolean value

See below to know that if each element of the array satisfies the rules you made, it will return true, as long as one of them is not satisfied, it will return false.

let arr = [1, 2, 3, 4];
console.log(arr);
let res = arr.every((item, index, arr) => {
    return item == 1;
})
console.log(res);

result

 5. some() Is there any condition in the array

  • Array name.some(function (item,index,arr) {})
  • item : each item of the array, index : the index of the array, arr : the original array
  • Function: Determine whether at least one of your array elements satisfies the condition, and the return value is a Boolean value

See below, as long as one is satisfied, it returns true, and if all are not satisfied, it returns false.

let arr = [1, 2, 3, 4];
console.log(arr);
let res = arr.some((item, index, arr) => {
    return item == 1;
})
console.log(res);

result 

 6. find() is used to get the first data that meets the condition in the array

  • Array name.find(function (item,index,arr) {})
  • item : each item of the array, index : the index of the array, arr : the original array
  • Function: used to obtain the data that meets the conditions in the array, if there is, it is the first data that meets the conditions; if not, it is undefined

See below, it is to return the first element that meets the condition.

let arr = [1, 2, 3, 4];
console.log(arr);
let res = arr.find((item, index, arr) => {
    return item >= 1;
})
console.log(res);

result

 7. The effect of reduce() superposition

  • Array name.reduce(function (item,index,arr) {})
  • prev: It is the initial value at the beginning when there is a result for the first time; this value is the result of the first time , item : each item of the array, index : the index of the array, arr : the original array
  • Function: used for superposition, the return value is the result of superposition.

PS: Do not write the prev value, the default is 0.

let arr = [1, 2, 3, 4];
console.log(arr);
let res = arr.reduce((prev, item, index, arr) => {
    return prev += item;
}, 10);
console.log(res);

result

Guess you like

Origin blog.csdn.net/Qhx20040819/article/details/132053895