jskatas.org Continuously Learn JavaScript. Your Way.

Array API: array.values()

Array.prototype.values() returns an iterator for all values in the array

Array.prototype.values returns an iterator for all values in the array

values() returns an iterator
const arr = ['k', 'e', 'y']; const iterator = arr.values(); assert.deepEqual(iterator.next(), {value: void 0, done: true});
use iterator.next() to drop first value
const arr = ['keys', 'values', 'entries']; const iterator = arr.values(); iterator.___(); assert.deepEqual([...iterator], ['values', 'entries']);
empty array contains no values
const arr = [...[...[...[...'1']]]]; const values = [...arr.values()]; assert.equal(values.length, 0);
a sparse array without real values has values though
const arr = [, 0]; const keys = [...arr.values()]; assert.deepEqual(keys, [void 0, void 0]);
also includes holes in sparse arrays
const arr = ['a',]; assert.deepEqual([...arr.values()], ['a', void 0, 'c']);

Required Knowledge

Related Katas

Array API

Difficulty Level

INTERMEDIATE

First Published

20 May 2015

Stats

5 tests to solve