JS Built-ins
← Array50 / 226
ArrayArray.prototype.entriesintermediate

Array.prototype.entries() β€” rebuild array from iterator

πŸ“– Usage Example
const arr = ['x', 'y', 'z']
const pairs = [...arr.entries()]
pairs  // β†’ [[0,'x'], [1,'y'], [2,'z']]

Spread entries() into an array to convert it to an array of [index, value] pairs.

Array.prototype.entries() β€” consuming an iterator

The iterator returned by entries() can be consumed step by step. You can use reduce on the array from the iterator to rebuild the original array from its entries.

Challenge: Implement rebuildFromEntries(arr) that iterates over arr.entries() and rebuilds the array using reduce.

rebuildFromEntries([10, 20, 30]) // β†’ [10, 20, 30]
πŸ’‘ Hints (2)
  • β€’ Convert the iterator first: `Array.from(arr.entries())` gives `[[0, val0], [1, val1], ...]`.
  • β€’ In the `reduce` callback, destructure as `[, val]` to skip the index.