What are the methods for array traversal?

When writing code, see array traversal, mind emerge first must be a for loop For this method, you must is very familiar with, to get the array, a see to traverse, picked up a for loop is doing, he is there any other method, the blogger before also is such, but since come into contact with the other methods, I can smell sweet. Let’s take a look at some useful methods.

1.forEach

The forEach() method is used to call each element of the array and pass the element to the callback function.

Note that forEach() does not perform callbacks on empty arrays.

// Give an example
let arr = [1.2.3]
/** Three arguments received by the forEach callback * currentValue: required. Current element * index: optional. The index value of the current element. * ARR: Optional. The array object to which the current element belongs. * * /
arr.forEach((currentValue, index, arr) = > {
  console.log(currentValue, index, arr)
})
/ print results: * * * 1, 0, 1, 2, 3 * 2, 1, 1, 2, 3 * 3, 2, and [1, 2, 3] * * /
Copy the code

One thing to note about forEach is that forEach can’t return to end a loop like a for loop, nor can it break or continue, but we can use a try catch to get it out of the loop, Let’s try and catch the problem

2.map

The map() method returns a new array whose elements are the values processed by the call to the original array element.

The map() method processes the elements in their original array order.

// Give an example
let arr = [1.2.3]
/** The map method callback also receives three arguments * currentValue: required. Current element * index: optional. The index value of the current element. * ARR: Optional. The array object to which the current element belongs. * * /
let res = arr.map((currentValue, index, arr) = > {
  return currentValue * 2
})
/** print the result: * [2,4,6] **/
Copy the code

The map method is like I take each value in the array and I manipulate it. So if you do an operation on it, and you get the value, of course it doesn’t change the array, it returns a new array

3.filter

The filter() method creates a new array of elements by checking all eligible elements in the specified array.

Note: Filter () does not detect an empty array. Note: Filter () does not change the original array.

// Give an example
let arr = [1.2.3]
/** The filter method callback also receives three arguments * currentValue: required. Current element * index: optional. The index value of the current element. * ARR: Optional. The array object to which the current element belongs. * * /
let res = arr.filter((currentValue, index, arr) = > {
  return currentValue > 1
})
/** print result: * [2,3] **/
Copy the code

The filter method acts as a filter, and returns an array containing all the elements that meet the criteria. An empty array is returned if no element matches the criteria.

4.some

The some() method is used to check whether elements in an array satisfy the specified condition (provided by the function).

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 checked. If no element satisfies the condition, false is returned.

Note: Some () does not check for empty arrays. Note: Some () does not alter the original array.

// Give an example
let arr = [1.2.3]
/** The callback function in the some method also receives three arguments * currentValue: required. Current element * index: optional. The index value of the current element. * ARR: Optional. The array object to which the current element belongs. * * /
let res = arr.some((currentValue, index, arr) = > {
  return currentValue === 1
})
let boolen = arr.some((currentValue, index, arr) = > {
  return currentValue === 5
})
/** Prints the result: * true * false **/
/**some (); /**some (); /**some (); /**some ()
Copy the code

The some method returns true as long as one of the values in the array meets the criteria, and false otherwise. For example, some brothers go on blind dates. Before a blind date, they have their own requirements. As long as a blind date meets the requirements, they will accept them and will not continue the blind date.

5.every

The every() method is used to check whether all elements of an array meet the specified criteria (provided by a function).

The every() method uses the specified function to detect all elements in the array: if one element in the array is detected as unqualified, the entire expression returns false, and the remaining elements are not tested. Returns true if all elements satisfy the condition.

Note: every() does not check for empty arrays. Note: every() does not change the original array.

// Give an example
let arr = [1.2.3]
/** the callback function in the every method also receives three arguments * currentValue: required. Current element * index: optional. The index value of the current element. * ARR: Optional. The array object to which the current element belongs. * * /
let every = arr.every((currentValue, index, arr) = > {
  return currentValue > 0
})
let boolen = arr.every((currentValue, index, arr) = > {
  return currentValue > 1
})
/** Prints the result: * true * false **/
Copy the code

Every little brother is more proud of jiao, must all meet my requirements, or I will quit.

6.find

The find() method returns the value of the first element of the array that passes the test.

The find() method calls the function execution once for each element in the array: when the element in the array returns true when testing the condition, find() returns the element that matches the condition, and values after #### do not call the execution function again. Return undefined if no element is found. Note: find() is not executed for an empty array.

Note: find() does not change the original value of the array.

// Give an example
let arr = [1.2.3]
/** The callback function in the find method also receives three arguments * currentValue: required. Current element * index: optional. The index value of the current element. * ARR: Optional. The array object to which the current element belongs. * * /
let res = arr.find((currentValue, index, arr) = > {
  return currentValue === 1
})
let res1 = arr.find((currentValue, index, arr) = > {
  return currentValue === 6
})
/** Result: * 1 * undefined **/
Copy the code

The find method is similar to some methods, except that it gets a value. In this case, it not only finds a match that meets the requirements, but also takes them home

7.findIndex

The findIndex() method returns the location of the first element of the array passed in to test the condition.

The findIndex() method calls function execution once for each element in the array: When the element in the array returns true when testing the condition, findIndex() returns the index position of the element that matches the condition, and the subsequent value does not call the execution function. Returns -1 if no element matches the criteria

Note: findIndex() does not execute for an empty array. Note that findIndex() does not change the original value of the array.

// Give an example
let arr = [1.2.3]
The findIndex method callback also receives three arguments * currentValue: required. Current element * index: optional. The index value of the current element. * ARR: Optional. The array object to which the current element belongs. * * /
let res = arr.findIndex((currentValue, index, arr) = > {
  return currentValue === 1
})
let res1 = arr.findIndex((currentValue, index, arr) = > {
  return currentValue === 6
})
/** Prints the result: * 0 * -1 **/
Copy the code

FindIndex is similar to find. It does not get a value, but an index of a value

8.reduce

The reduce() method takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and eventually evaluates to a value. Reduce () can be used as a higher-order function for compose of functions.

Note: Reduce () does not perform a callback for an empty array.

// Give an example
let arr = [1.2.3.4]
/** Four arguments received by the reduce callback function * total: required. The initial value, or the return value at the end of the calculation. * currentValue: required. Current element * currentIndex: Optional. The index value of the current element. * ARR: Optional. The array object to which the current element belongs. * reduce Second parameter initialValue: optional. The initialValue passed to the function (if no initialValue is provided, reduce executes the callback method starting at index 1, skipping the first index. If initialValue is provided, start at index 0. * * /
var sum = arr.reduce(function(prev, cur, index, arr) {
    console.log(prev, cur, index);
    returnprev + cur; },0)
console.log(arr, sum);
0 10 1 2 1 3 3 2 6 4 3 [1, 2, 3, 4] 10 **/
Copy the code

There are many advanced uses of reduce, such as array de-duplication and array flattening, but we will only cover the simple ones here

Second, the epilogue

For array traversal method there are many, I will not be introduced, although these things are simple, but tidy up is still good, for small white or reference value, of course, or hope big guy do not spray. Ha ha ha ha