In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what the new traversal method of es6 is, which is very detailed and has certain reference value. Friends who are interested must finish it!
The new traversal methods are: 1, findIndex (), traversing the array to find matching elements; 2, find (), traversing the array to find the first matching element; 3, entries (), traversing the key value pair; 4, keys (), traversing the key name; 5, values (), traversing the key value.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
10 traversal methods commonly used in ES5:
1. The original for loop statement
2. Array.prototype.forEach array object built-in method
3. Built-in method of Array.prototype.map array object
4. Array.prototype.filter array object built-in method
5. Array.prototype.reduce array object built-in method
6. Array.prototype.some array object built-in method
7. Array.prototype.every array object built-in method
8. Array.prototype.indexOf array object built-in method
9. Array.prototype.lastIndexOf array object built-in method
10. For...in loop statement
Traversal method added by es6
FindIndex (callback [, thisArg]) finds matching elements in the array
If one is found, the subscript of the matching element is returned, and if it is not found,-1 is returned. Let arr = [1, 2, 3, 4, 5, 6] / / at this time we find the number let newArr = arr.findIndex (val = > {return val > 2}) console.log (newArr) / / 2
Find (fn (callback [, thisArg])
Look for matching elements in the array, find one, return matching elements, and return undefined if you don't find one.
Note: the following example is a false demonstration relative to the requirement, because we need to find a number greater than 2, and when a match is found to 3, the function will stop when the condition is met.
Example:
Let arr = [1, 2, 3, 4, 5, 6] / / at this time we find the number let newArr = arr.find (val = > {return val > 2}) console.log (newArr) / / 3
Entries (), keys () and values ()
ES6 provides three new methods-- entries (), keys (), and values ()-- for traversing arrays and objects. They all return a traversal object that can be traversed with a for...of loop, except that keys () is the traversal of the key name, values () is the traversal of the key value, and entries () is the traversal of the key-value pair.
For (let index of ['asides,' b'] .keys ()) {console.log (index);} / 0Accord / 1for (let elem of ['asides,' b'] .values ()) {console.log (elem);} / / 'b'for (let [index, elem] of [' asides,'b'] .keys ()) {console.log (index, elem);} / 0 "a" / / 1 "b"
If you do not use a for...of loop, you can manually call the next method of the traversal object to traverse.
Let letter = ['averse,' baked,'c']; let entries = letter.entries (); console.log (entries.next () .value); / / [0,'a'] console.log (entries.next () .value); / / [1,'b'] console.log (entries.next () .value) / / [2,'c'] these are all the contents of the article "what is the new traversal method for es6". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.