Array traversal method in JavaScript

1. forEach

grammar:array.forEach(function(item, index, origin){})
parameter:

  1. The actual value of each item in the array
  2. The subscript corresponding to each item of the array
  3. Original array
    Function: Traversing the array
    Return value: The return value is undefined If you write return, the return value is also undefined
var arr = [1,2,3,4,5]
var num = arr.forEach(function(item,index,origin){
    
    
          return item})
console.log(num)  //---> 原数组

2. map

grammar:array.map(function (item, index, origin) {})
parameter:

  1. The actual value of each item in the array
  2. The subscript corresponding to the actual value of each item in the array
  3. Original array
    Function: Mapping array
    Return value: Return an array with the same length as the original array, and each value of the returned array depends on how to write the return of the parameter
var arr = [1,2,3,4,5]
var num = arr.map(function(item,index,origin){
    
    
          return item*2})
console.log(num)  //---> [2,4,6,8,10]

3. filter

grammar:array.filter(function (item, index, origin) {})
parameter:

  1. The actual value of each item in the array
  2. The subscript corresponding to the actual value of each item in the array
  3. Original array
    Function: Filter the array
    Return value: The new array after filtering the array, the filter condition depends on how to write the return of the parameter
var arr = [1,2,3,4,5]
var num = arr.filter(function(item,index,origin){
    
    
          return item > 2})
console.log(num)  //---> [3,4,5]

4. find

grammar:array.find(function (item, index, origin) {})
parameter:

  1. The actual value of each item in the array
  2. The subscript corresponding to the actual value of each item in the array
  3. Original array
    Function: Find data
    Return value: The first data found in the array (not an array)
var arr = [1,2,3,4,5]
var num = arr.find(function(item,index,origin){
    
    
          return item > 3})
console.log(num)  //---> 4

5. findIndex

grammar:array.findIndex(function (item, index, origin) {})
parameter:

  1. The actual value of each item in the array
  2. The subscript corresponding to the actual value of each item in the array
  3. Original array
    Function: Find the subscript of the first occurrence of the data
    Return value: The subscript of the first occurrence of the data found in the array
var arr = [1,2,3,4,5]
var num = arr.findIndex(function(item,index,origin){
    
    
          return item > 3})
console.log(num)  //---> 3

6. every

grammar:array.every(function(item, index, origin){})
parameter:

  1. The actual value of the first item in the array
  2. The subscript corresponding to the actual value of the first item in the array
  3. Original array
    Function: Determine whether all the data in the array meet the conditions
    Return value: a Boolean value
    true —> all the data in the array meet the conditions
    false —> at least one data in the array does not meet the conditions
var arr = [1,2,3,4,5]
var num = arr.every(function(item,index,origin){
    
    
          return item > 0})
console.log(num)  //---> true

7. some

grammar:array.some(function (item, index, origin) {})
parameter:

  1. The actual value of each item in the array
  2. The subscript corresponding to the actual value of each item in the array
  3. Original array
    Function: Determine whether there is an item in the array that satisfies the condition
    Return value: A Boolean value
    true: indicates that at least one item in the array meets the condition
    false: none of the data in the array meets the condition
var arr = [1,2,3,4,5]
var num = arr.some(function(item,index,origin){
    
    
          return item > 5})
console.log(num)  //---> false

8. reduce

grammar:array.reduce(function (prve, item, index, origin) {}, init)
The parameters of the reduce method:

  1. If the second parameter init is passed, the number of executions is the same as the length of the array
  2. If the second parameter init is not passed, the default first value is the value of the first item of the array, and the number of executions is reduced by 1 from the length of the array. The
    meaning of the 4 formal parameters in the function of parameter 1:
  3. Represents the initial value or the value of the first item of the array (depending on whether init is passed)
  4. The actual value of each item in the array
  5. The subscript corresponding to the actual value of each item in the array
  6. Original array
    Function: Accumulation (superposition)
    Return value: The value obtained after the loop runs

do not pass the second parameter

var arr = [1,2,3,4,5]
var num = arr.reduce(function(prve,item,index,origin){
    
    
          return prve + item})
console.log(num)  //---> 15

Pass the second parameter

var arr = [1,2,3,4,5]
var num = arr.reduce(function(prve,item,index,origin){
    
    
          return prve * item},2)
console.log(num)  //---> 240

Guess you like

Origin blog.csdn.net/weixin_48649246/article/details/127540320