JavaScript Array find()

Basics

The find() method searches through an array and gives you 1 item based on your provided test. Array find() only returns one item. If you want to get more than one item, look at filter().

Here we'll find one person based on their name:

['chris', 'nick', 'holly'].find(function(name) {
    return name === 'holly';
});

// or simplified with es6 arrow functions
['chris', 'nick', 'holly'].find(name => name === 'holly');

Finding an Object in an Array

You can also filter through an array of objects:

Syntax

const foundItem = myArray.find(callbackFunction);

2 Parameters

callbackFunction (function)

The function to execute over each of the elements of the array. When the function returns true , it has found its item.

The callback function takes 3 parameters.

  • element: this is the element currently being executed - required.
  • index: the index of the current item - optional.
  • array: the array that is currently being processed - optional.

thisArgument (this) (optional)

We can also pass an argument as the second parameter to be used as the this value in the callback

Returns a single array item

Array find() will return the single item that it finds. This can be a string, object, or any item in the array.

Common Uses and Snippets

Finding the first matching element in an array

We'll look through this list and find() the first number that is less than 50. Array find() only returns one item. If you want to get more than one item, look at filter().

Getting an item in array at a certain index.