How to use Lodash to find & Return an Object from Array ?
Last Updated :
21 Mar, 2024
In JavaScript, the Lodash Module has different methods of doing and get objects from the array. we will explore three different methods with practical implementation of each approach in terms of examples and output to to find and return an object from Array.
These are the following methods:
Using _.find() method
In this approach, we are using the _.find() method from lodash library to search for an object in the array 'arr' where the 'name' property matches 'GeeksforGeeks'. This method returns the first object that satisfies the given condition, or undefined if none is found.
Syntax:
_.find(collection, predicate, [fromIndex=0])
Example: The below example uses the _.find() method to find and return an object from Array.
JavaScript
const _ = require('lodash');
const arr = [
{
id: 1, name: 'GeeksforGeeks',
category: 'Education'
},
{
id: 2, name: 'Stack Overflow',
category: 'Q&A'
},
{
id: 3, name: 'GitHub',
category: 'Development'
}
];
function approach1Fn(arr, obj) {
return _.find(arr, obj);
}
const temp = { name: 'GeeksforGeeks' };
const res = approach1Fn(arr, temp);
console.log(res);
Output:
{ id: 1, name: 'GeeksforGeeks', category: 'Education' }
Using _.findIndex and _.nth methods
In this approach, we are using the _.findIndex() method from lodash to determine the index of the object in the array 'arr' where the 'name' property matches 'GeeksforGeeks'. Then, we use _.nth() to get the object at that index.
Syntax:
_.findIndex(array, [predicate=_.identity], fromIndex);
_.nth(array, n);
Example: The below example uses _.findIndex and _.nth methods to find and return an object from Array.
JavaScript
const _ = require('lodash');
const arr = [
{
id: 1, name: 'GeeksforGeeks',
category: 'Education'
},
{
id: 2, name: 'Stack Overflow',
category: 'Q&A'
},
{
id: 3, name: 'GitHub',
category: 'Development'
}
];
function approach2Fn(arr, key, value) {
const ind = _.findIndex(arr, [key, value]);
if (ind !== -1) {
return _.nth(arr, ind);
} else {
return null;
}
}
const res = approach2Fn(arr, 'name',
'GeeksforGeeks');
console.log(res);
Output:
{ id: 1, name: 'GeeksforGeeks', category: 'Education' }
Using _.filter() method
In this approach, we are using the _.filter() method from lodash to create a new array containing objects from 'arr' where the 'name' property matches 'GeeksforGeeks'.
Syntax:
_.filter( collection, predicate )
Example: The below example uses the _.filter() method to find and return an object from Array.
JavaScript
const _ = require('lodash');
const arr = [
{
id: 1, name: 'GeeksforGeeks',
category: 'Education'
},
{
id: 2, name: 'Stack Overflow',
category: 'Q&A'
},
{
id: 3, name: 'GitHub',
category: 'Development'
}
];
function approach3Fn(arr, key, value) {
return _.filter(arr, [key, value])[0];
}
const res = approach3Fn(arr, 'name',
'GeeksforGeeks');
console.log(res);
Output:
{ id: 1, name: 'GeeksforGeeks', category: 'Education' }
Similar Reads
How to use Lodash to Find & Return an Object from Array ? JavaScript's built-in array methods offer basic functionality whereas Lodash, a popular utility library, empowers developers with robust tools for complex array operations. Below are the methods to find and return an object from array using Lodash: Table of Content Using _.find()Using _.findIndex()
3 min read
How to Find & Update Values in an Array of Objects using Lodash ? To find and update values in an array of objects using Lodash, we employ utility functions like find or findIndex to iterate over the elements. These functions facilitate targeted updates based on specific conditions, enhancing data manipulation capabilities.Table of ContentUsing find and assign Fun
4 min read
How to Convert Object to Array in Lodash ? Converting an Object to an Array consists of changing the data structure from key-value pairs to an array format. Below are the different approaches to converting objects to arrays in Lodash: Table of Content Using toArray function Using values functionRun the below command before running the below
2 min read
How to Find Property by Name in a Deep Object Using Lodash? When working with deeply nested objects in JavaScript, finding a specific property can be challenging. Using Lodash, a powerful utility library, simplifies this task with its robust set of functions. This guide explores how to effectively search for a property by name within a deeply nested object u
2 min read
How to Convert Object Array to Hash Map using Lodash ? Converting an Object Array to a Hash Map consists of manipulating the array elements to create a key-value mapping. Below are the different approaches to Converting an object array to a hash map using Lodash:Table of Content Using keyBy() functionUsing reduce functionRun the below command before run
2 min read
How to Filter Array of Objects with Lodash Based on Property Value? Sometimes, We need to filter an array of objects based on specific property values. Lodash, a powerful utility library, provides efficient methods for such operations. we will explore how to use Lodash to filter an array of objects by property value, ensuring that you can easily extract and work wit
2 min read