JavaScript Find Object In Array By Property Value

JavaScript Find Object In Array By Property Value | Finding an object in an array by property value is a common operation in JavaScript. This can be useful when you want to search for an object that has a specific value for one of its properties. In this article, we will explore the different ways to find an object in an array by property value in JavaScript. Also see:- JavaScript Object Get Value By Key

JavaScript Find Object In Array By Property Value Using For Loop

The most basic method to find an object in an array by property value is to use a for loop. You can iterate through the array and check each object’s property value to see if it matches the desired value. Here’s an example:-

let arr = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 3, name: "Jim" },
];

let result = null;
for (let i = 0; i < arr.length; i++) {
  if (arr[i].name === "Jane") {
    result = arr[i];
    break;
  }
}

console.log(result);

Output:-

{ id: 2, name: ‘Jane’ }

In the above example, the for loop iterates through the array and checks each object’s name property. When the name property matches ‘Jane’, the object is stored in the result variable and the loop is broken.

JavaScript Find Object In Array By Property Value Using Array.prototype.find()

The Array.prototype.find() method is another way to find an object in an array by the property value. This method returns the first element in the array that satisfies the provided testing function. Here’s an example:-

let arr = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 3, name: "Jim" },
];

let result = arr.find((obj) => obj.name === "Jane");
console.log(result);

Output:-

{ id: 2, name: ‘Jane’ }

In the example above, the Array.prototype.find() method is used to search for the object in the array that has a name property equal to ‘Jane’. The method returns the first object that satisfies the condition, which is stored in the result variable.

JavaScript Find Object In Array By Property Value Using Array.prototype.filter()

The Array.prototype.filter() method is another option for finding an object in an array by the property value. This method returns a new array with all elements that pass the test implemented by the provided function. Here’s an example:-

let arr = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 3, name: "Jim" },
];

let result = arr.filter((obj) => obj.name === "Jane");
console.log(result);

Output:-

[ { id: 2, name: ‘Jane’ } ]

In the example above, the Array.prototype.filter() method is used to filter the array and return all objects that have a name property equal to ‘Jane’. The method returns an array with all matching objects, which is stored in the result variable.

Using Array.prototype.findIndex()

The Array.prototype.findIndex() method is another option for finding an object in an array by the property value. This method returns the index of the first element in the array that satisfies the provided testing function. Here’s an example:-

let arr = [
  { name: "John", age: 30 },
  { name: "Jane", age: 25 },
];
let result = arr.map((obj) => obj.name).indexOf("Jane");
console.log(result);

Output:-

1

Lodash_.findIndex()

Lodash is a popular JavaScript utility library that provides many useful functions, including the _.findIndex method. This method is similar to the Array.prototype.map method, but it’s more convenient to use. Here’s an example.

Before using the below code, lodash module must be installed. To install the lodash, we can use the command:- npm install lodash.

const _ = require("lodash");
let arr = [
  { name: "John", age: 30 },
  { name: "Jane", age: 25 },
];
let result = _.findIndex(arr, { name: "Jane" });
console.log(result);

Output:-

1

In conclusion, finding an object in an array by its property value is a common operation in JavaScript. The for loop, Array.prototype.find(), Array.prototype.filter(), Array.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *