JS Built-ins
← Array51 / 226
ArrayArray.prototype.entriesadvanced

Array.prototype.entries() β€” combine arrays into an object

πŸ“– Usage Example
const arr = ['a', 'b', 'c']
const obj = Object.fromEntries(arr.entries())
// β†’ { 0: 'a', 1: 'b', 2: 'c' }

Pass entries() to Object.fromEntries() to convert an array into an indexed object.

Array.prototype.entries() β€” creating key-value maps

You can use entries() on a keys array to pair indices with values from a parallel values array, building an object map.

Challenge: Implement arrayToObject(keys, vals) that combines two parallel arrays into a plain object using entries().

arrayToObject(['a', 'b', 'c'], [1, 2, 3]) // β†’ { a: 1, b: 2, c: 3 }
πŸ’‘ Hints (2)
  • β€’ Iterate over `keys.entries()` to get `[index, key]` β€” then use `vals[index]` for the value.
  • β€’ You can also use `Object.fromEntries(keys.map((k, i) => [k, vals[i]]))` for a one-liner.