jskatas.org Continuously Learn JavaScript. Your Way.

Array API: array.entries()

Array.prototype.entries() returns an iterator object with all entries.

[].entries() returns an iterator object with all entries

returns key+value for each element
const arr = ['a', 'b', 'c']; const entriesAsArray = Array.from(arr.___()); assert.deepEqual(entriesAsArray, [[0,"a"], [1,"b"], [2,"c"]]);
empty elements contain the value undefined
const arr = ['one']; arr[2] = 'three'; const secondValue = arr.entries(); assert.deepEqual(secondValue, [1, void 0]);

returns an iterable

has next() to iterate
const arr = ['one']; const value = arr; assert.deepEqual(value, [0, 'one']);

Required Knowledge

Related Katas

Array API

Difficulty Level

INTERMEDIATE

First Published

18 May 2015

Stats

3 tests to solve