JS Built-ins
← Array47 / 226
ArrayArray.prototype.entriesbeginner

Array.prototype.entries() β€” convert to array

πŸ“– Usage Example
const arr = ['a', 'b', 'c']
const iter = arr.entries()
iter.next().value  // β†’ [0, 'a']
iter.next().value  // β†’ [1, 'b']

Use entries() to get an iterator of [index, value] pairs from an array.

Array.prototype.entries()

Array.prototype.entries() returns an iterator of [index, value] pairs for every element. You must consume the iterator β€” for example with Array.from() β€” to get a plain array.

Challenge: Implement getEntries(arr) that converts arr.entries() to a regular array of [index, value] pairs using Array.from().

getEntries(['a', 'b', 'c']) // β†’ [[0, 'a'], [1, 'b'], [2, 'c']]
getEntries([])              // β†’ []
πŸ’‘ Hints (2)
  • β€’ `arr.entries()` returns an iterator β€” wrap it with `Array.from()` to get a regular array.
  • β€’ Each yielded value is a `[index, element]` tuple.