JS Built-ins
← Array214 / 226
ArrayArray.prototype.valuesintermediate

Compare values() and keys()

πŸ“– Usage Example
const arr = ['a', 'b', 'c']
[...arr.keys()]    // β†’ [0, 1, 2]     (indices)
[...arr.values()]  // β†’ ['a','b','c'] (values)

Use values() to iterate element values and keys() to iterate indices.

Array.prototype.values β€” vs keys()

arr.keys() yields the indices, while arr.values() yields the elements. Both are iterators and work with Array.from.

Implement valuesAndKeys which returns an object with both keys and values arrays derived from the respective iterators.

πŸ’‘ Hints (2)
  • β€’ arr.keys() yields 0, 1, 2, ... β€” the indices
  • β€’ arr.values() yields the actual element values at each index