Javascript - Array iterative method of learning

First, the array iterative method

1、forEach

transfer

arr.forEach((value, index) => {
  console.log(value, index)
})

forEachAre each loop through the array again, this is no return value of the method which has a callback, the callback function has two parameters, the first one is a value of each array value, each of the second parameter is an array an index corresponding to the indexindex

Tips This forEahcloop through and JQuery the $.each()method is similar, except to note that two parameters with $ .each method inside the callback function forEachis to the contrary, the first parameter is an array subscript index of each item index, the second parameter is an array of values for each itemvalue

2、every

Method Reference

transfer

const boolean = arr.every((value, index, arr) => {
  return value==2
})

This is somewhat similar to the way an array of short circuit operations &&, can be regarded as a true test array method, this method which has a callback, the callback function takes three parameters, the first test of each item is an array, the second is a test subscript index array item, and the third is the original array testing

Each one will perform the test array every()method inside the callback function that is each meets the specified criteria used to verify the array, the array will be executed each time the callback function, if inside the array for all items that meet the specified conditions returns true, whenever there is an array conditions are not satisfied, returnfalse

Tips If the test is an empty array, it will return in all cases true, this method does not change the original array

3、some

Method Reference

transfer

const boolean = arr.some((value, index, arr) => {
  return value==2
})

This method is somewhat similar to an array of short circuit operations ||, can be used as a test method for an array false, this method which has a callback, the callback function takes three parameters, the first test of each item is an array, the second is a test each array subscript index, and the third is the original array testing

Each one will perform the test array some()method inside the callback function that is used to verify whether each item in the array has a specified condition is met, the array will be executed every time the callback function, if an array of all items inside does not meet the specified criteria, returns false, whenever there is an array condition is met, immediately returnstrue

Tips If the test is an empty array, it will return in all cases false, note that this method does not find an array of items to satisfy a condition, it will have to perform it, if it finds an array of items that satisfies the conditions, will be returned immediately true, behind data will no longer perform

4、map

Method Reference

transfer

const newArr = arr.map((value, index, arr) => {
  return value==2
})

This array method returns a new array, the resulting array is a collection of results after each item in the array callback function, the callback function of this method has three parameters, the first is the array of each test, and the second is each of the test array subscript index, and the third is the original array testing

Each one will test an array of callback function, and the implementation of the results is appended to a new array inside, and then return the new array, this after each new array is a callback function result set

Tips this approach and will change the original array, but may callbackchange the original array execution

5、filter

Reference Function

transfer

const newArr = arr.filter((value, index, arr) => {
  return value==2
})

This method returns a new array array, a new array is the result after each performed a callback function test filter array filters satisfies the condition of the result set, the callback function of the three parameters of this method, a first test each array is item, the second is to test each array subscript index, and the third is the original array testing

This method is equivalent to a filter, the filter, the test array will specify each callback function, but only results that meet the specified items can be added to the new array, and return, not satisfied it is filtered out, if are not met, an empty array is returned

Tips will not change the original array, it returns the result of the new filter array

6、reduce

Method Reference

transfer

const result = arr.reduce((count, value, index, arr) => {
  return count + value
},initCount)

This array method will return a result value for each accumulated an array, this method has two parameters:

The first is a callback, the callback function has four parameters, the first array entry is executed every time when the accumulated result of executed, or the initial user-defined integration result, the second is to test each array item, and the third is to test each array subscript index, and the fourth is an array of original test

The second parameter is the initial accumulated result of this is a user-defined, using this value, if defined, is executed to initialize the accumulated first array, and the result of the accumulated value as the first argument to the callback function the second cumulative argument, followed by the cycle continues, and if not, then by default the first item in the array

Methods similarities and differences:

1, forEachare each an array traversal cycle, does not return results, reduceit is a total of each array and return the final cumulative value

2, everyand somereturns a Boolean value, the two are opposite, the former is all true, it returns true, does not terminate the term after execution, which is as long as there is a true, immediate return true, and after the closing of entries carried out

3, mapand filterreturns a new array, return the former is a calculation result of each array of the array, which is a calculation result return an array that satisfies Condition

4, the above method does not change the original array

Guess you like

Origin www.cnblogs.com/zjh-study/p/10954104.html