Open In App

How to use Lodash to find & Return an Object from Array ?

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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' }


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads