Array Array traversal
map() Method to create a new array , The result is that each element in the array is the return value after a call to the supplied function .
- When you don't want to use the returned new array, you use
mapIt is against the original intention of the design , Please use
- You shouldn't use it
map: A) You're not going to use the new array returned , or / And B) You didn't return a value from the callback function ;
mapDo not modify the original array itself that calls it （ Of course, it can be in
callbackChange the original array when executing ）;
Return valueA new array consisting of the results of the callback function executed by each element of the original array .
forEach() Method performs the given function once for each element of the array .
forEach()Method is executed once in ascending order for each item in the array that contains a valid value
callbackfunction , Items that have been deleted or uninitialized will be skipped （ For example, on a sparse array ）
forEach()When called , It doesn't change the original array , That is, the array that calls it （ Even though
callbackFunction may change the original array when it is called ）
In addition to throwing exceptions , There is no way to stop or jump out
If you need to terminate the loop early , You can use ：
These array methods can judge the array elements , To determine if you need to continue traversing ：
translator's note ： As long as conditions permit , You can also use
filter()Filter out the parts to be traversed in advance , Reuse
forsentence Used to create a loop , It contains three optional expressions , These three expressions are enclosed in parentheses , Use semicolons to separate , Followed by a statement for execution in the loop （ It's usually a Block statement ）.
forAll three expressions in parentheses at the beginning of the statement are optional .
- Of course you can ignore all expressions . alike , Make sure you use
breakStatement to jump out of the loop and modify （ increase ） A variable , bring break The condition of the statement is true at some time .
for...of(IE Are not compatible )
for...of sentence stay Iteratable object （ Include
TypedArray,arguments Objects, etc. ） Create an iteration loop on , Call the custom iteration hook , And execute statements for the values of each different property
for...ofThe cycle of , Can be
returnEnd . In these cases , Iterator off .
for...inThe difference between
- Tips ：
for...inIt should not be used to iterate over a that focuses on index order
for ... inIs built to traverse object properties , Not recommended with arrays , Arrays can use
for ... of;
Array.prototype.find()(IE Are not compatible )
find() Method returns the value of the first element in the array that satisfies the provided test function . Otherwise return to
- See also
findIndex()Method , It returns the index of the elements found in the array , Not its value .
findMethod does not change the array .
Return valueThe value of the first element in the array that satisfies the provided test function , Otherwise return to
filter() Method to create a new array , It contains all the elements of the test implemented by the provided function .
Return valueA new 、 An array of elements that pass the test , If no array elements pass the test , Then return an empty array .
filterIt doesn't change the original array , It returns the filtered new array .
- Array traversal does not need to be used when exiting the loop
- When array traversal needs to generate a new array, use
map(), Use... When no return value is required
- Array traversal needs to be used when exiting the loop
for ... in、
find()IE Are not compatible ;
- Use... When filtering array elements