Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Array traversal by es6

Shulou Source: shulou.com Published: 2022-05-31 18:39:29 09月20日 Update

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!

Tags: Arrays methods elements indexes results learning entries properties more help practical and then tutorials articles environment theory computers knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia macOS Shulou Information MySQL