9951 explained code solutions for 126 technologies


javascript-lodashHow do I use Lodash to find an object in an array of JavaScript objects?


Using Lodash to find an object in an array of JavaScript objects is simple and straightforward. The following example code block shows how to use Lodash's _.find method to find an object in an array of JavaScript objects:

const users = [{
  'user': 'barney',
  'age': 36
}, {
  'user': 'fred',
  'age': 40
}, {
  'user': 'pebbles',
  'age': 1
}];

const user = _.find(users, { 'age': 36 });

console.log(user);

Output example

{
  'user': 'barney',
  'age': 36
}

The code above first creates an array of JavaScript objects called users and then uses Lodash's _.find method to find an object that has an age property with the value of 36. The _.find method returns the first object in the array that matches the criteria, and in this case it returns the object with user property set to barney and age property set to 36.

Code explanation

  1. const users = [{...}, {...}, {...}]; - creates an array of JavaScript objects called users
  2. const user = _.find(users, { 'age': 36 }); - uses Lodash's _.find method to find an object with an age property set to 36
  3. console.log(user); - prints the found object to the console

Helpful links

Edit this code on GitHub