In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "es6 how to achieve array traversal". In daily operation, I believe many people have doubts about how to achieve array traversal in es6. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how es6 realizes array traversal". Next, please follow the editor to study!
Methods: 1, using the "array .keys ()" method, this method can traverse the array index; 2, using the "array .values ()" method, this method can traverse the array elements; 3, using the "array .keys ()" method, this method can traverse the array index and elements.
This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.
How to implement Array traversal by es6
ES6 provides an entries (), keys (), values () method to return an array of traversers, which can be conveniently done by using for...of for the Iterator, or by using the Iterator.next () method returned by entries ().
1. Use keys () to traverse.
Keys () returns the traversal of the index number of the array element.
Const arr1 = ['console.log,' baked, 'cased,' dashed, 'eBay,' faded, 'galled,' hacked, 'ified,' jacked,'k'] for (let index of arr1.keys ()) {console.log (index);}
Results:
You can see that the output is the index of each array element.
0 1 2 3 4 5 6 7 8 9 10
two。 Use values () to traverse.
Values () returns a traversal of the values of the array elements.
For (let val of arr1.values ()) {console.log (val);}
Results:
A b c d e f g h i j k
3. Use entries () to traverse.
With deconstruction, you can get the index and value of the element.
For (let [index, val] of arr1.entries ()) {console.log (index, val);}
Results:
0'a, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 9, 10, 10, 000.
4. Use Iterator.next () to traverse.
Based on the traversal returned by entries (), you can get the access entry for each element by calling the next () method of the traversal, which has a done attribute indicating whether it is convenient to end. Through the entry, you can get the value attribute, which is the index of the element and an array of values.
Let arrEntries=arr1.entries (); let entry=arrEntries.next (); while (! entry.done) {console.log (entry.value); entry=arrEntries.next ();}
Results:
[0,'a'] [1,'b'] [2,'c'] [3,'d'] [4,'e'] [5,'f'] [6,'g'] [7,'h'] [8,'i'] [9,'j'] [10,'k'] this ends the study of "how es6 implements array traversal" I hope I can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.