Summary of array traversal

mansion 2021-09-15 10:51:21

Array Array traversal

Array.prototype.map()(IE9+)

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 .

  1. When you don't want to use the returned new array, you use map It is against the original intention of the design , Please use forEach perhaps for-of replace ;
  2. 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 ;
  3. map  Do not modify the original array itself that calls it ( Of course, it can be in  callback  Change the original array when executing );
  4. Return value A new array consisting of the results of the callback function executed by each element of the original array .

Array.prototype.forEach()(IE9+)

forEach()  Method performs the given function once for each element of the array .

  1. forEach()  Method is executed once in ascending order for each item in the array that contains a valid value  callback  function , Items that have been deleted or uninitialized will be skipped ( For example, on a sparse array )

  2. forEach()  When called , It doesn't change the original array , That is, the array that calls it ( Even though  callback  Function may change the original array when it is called )

  3. In addition to throwing exceptions , There is no way to stop or jump out  forEach()  loop

  4. If you need to terminate the loop early , You can use :

  5. These array methods can judge the array elements , To determine if you need to continue traversing :

  6. translator's note : As long as conditions permit , You can also use  filter()  Filter out the parts to be traversed in advance , Reuse  forEach()  Handle .

  7. Return value undefined.

for(IE3+)

  1. for  sentence 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 ).
  2. for  All three expressions in parentheses at the beginning of the statement are optional .
  3. Of course you can ignore all expressions . alike , Make sure you use  break  Statement 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  Array,Map,Set,String,TypedArray,arguments  Objects, etc. ) Create an iteration loop on , Call the custom iteration hook , And execute statements for the values of each different property

  1. about for...of The cycle of , Can be breakthrow  continue    or return End . In these cases , Iterator off .
  2. for...of And for...in The difference between
    • Whether it's for...in still for...of Statements are iterations of things . The main difference between them is the way they iterate .
    • for...in  Statement iterates through the objects in any order Enumerable properties .
    • for...of  Statement traversal Iteratable object Define the data to iterate over .

for...in(IE6+)

for...in sentence Traverse the division of an object in any order Symbol Outside of the enumerable attribute .

  1. Tips : for...in It should not be used to iterate over a that focuses on index order  Array.
  2. for ... in Is built to traverse object properties , Not recommended with arrays , Arrays can use Array.prototype.forEach() and 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  undefined.

  1. See also  findIndex()  Method , It returns the index of the elements found in the array , Not its value .
  2. find Method does not change the array .
  3. Return value The value of the first element in the array that satisfies the provided test function , Otherwise return to  undefined.

Array.prototype.filter()(IE9+)

filter()  Method to create a new array , It contains all the elements of the test implemented by the provided function .

  1. Return value A new 、 An array of elements that pass the test , If no array elements pass the test , Then return an empty array .
  2. filter  It doesn't change the original array , It returns the filtered new array .

Summary

  1. Array traversal does not need to be used when exiting the loop map()forEach() ;
  2. When array traversal needs to generate a new array, use map(), Use... When no return value is required forEach();
  3. Array traversal needs to be used when exiting the loop forfor...of/for ... infind();
  4. for...of And find()IE Are not compatible ;
  5. Use... When filtering array elements filter();
Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15