Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash

How to use lodash to find and return an object from array

abhilash

Overview

The _.find() method in lodash is used to locate objects in an array that meet a set of requirements.

This method loops through every element in the collection and returns the first element for which the callback returns true.

Syntax

_.find(collection, [callback=identity])

Parameters

  • collection: This is the collection to iterate over.
  • callback: This is the function that’s called per iteration.

Return value

The method returns the found element. Otherwise, it is undefined.

Code example

Let’s look at the code below:

Console

Code explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Line 1 to 5: We create an array of objects.
  • Line 7: We define the object to be located.
  • Line 8: We use the _.find() method to find the object to be located.
  • Line 9: We print the output to the console.

RELATED TAGS

lodash
RELATED COURSES

View all Courses

Keep Exploring