Array entries, keys, and values

ES2015 also introduced three ways of retrieving iterators from an array. The first one you will learn is the entries method.

The entries method returns @@iterator, which contains key/value pairs. The following is an example of how we can use this method:

let aEntries = numbers.entries(); // retrieve iterator of key/value 
console.log(aEntries.next().value); // [0, 1] - position 0, value 1 
console.log(aEntries.next().value); // [1, 2] - position 1, value 2 
console.log(aEntries.next().value); // [2, 3] - position 2, value 3 

As the number array only contains numbers, key will be the position of the array, and value will be the value stored in the array index.

We can also use the following code as an alternative ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.