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

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



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to find Length of Linked List using JavaScript JavaScript Program to find Length of Linked List using JavaScript
JavaScript Program to Check if Element Exists in Linked List JavaScript Program to Check if Element Exists in Linked List
JavaScript Program to Sort the 2D Array Across Rows JavaScript Program to Sort the 2D Array Across Rows
JavaScript Program to find Volume & Surface Area of Cuboid JavaScript Program to find Volume & Surface Area of Cuboid
JavaScript Program to Find Curved Surface Area of a Cone JavaScript Program to Find Curved Surface Area of a Cone

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12