jskatas.org Continuously Learn JavaScript. Your Way.

Array API: array.find()

Array.prototype.find() finds one item in an array.

Array.prototype.find makes finding items in arrays easier

takes a compare function
const found = [true].find(true); assert.equal(found, true);
returns the first value found
const found = [0, 1].find(item => item > 1); assert.equal(found, 2);
returns undefined when nothing was found
const found = [1, 2, 3].find(item => item === 2); assert.equal(found, void 0);
combined with destructuring complex compares become short
const bob = {name: 'Bob'}; const alice = {name: 'Alice'}; const found = [bob, alice].find(({name}) => name); assert.equal(found, alice);

Related Katas

Array API

Difficulty Level

TBD

First Published

29 April 2015

Stats

4 tests to solve