Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding matching objects in an array of objects?

Tags:

javascript

var set = [{"color":"blue"},{"color":"green"},{"color":"red"},{"color":"green"}]; 

I'd like to be able to do something like a db call, set.find({"color":"green"}) and have it return an array full of objects that contain that property.

like image 504
fancy Avatar asked Jun 04 '11 15:06

fancy


People also ask

How do you find a matching element in an array?

To find the first array element that matches a condition:Use the Array. find() method to iterate over the array. Check if each value matches the condition. The find method returns the first array element that satisfies the condition.

How do you match the value of an array of objects?

function findByMatchingProperties(set, properties) { return set. filter(function (entry) { return Object. keys(properties). every(function (key) { return entry[key] === properties[key]; }); }); } var results = findByMatchingProperties(set, { color: "green" });

How do you find the object of an array of objects?

Answer: Use the find() Method You can simply use the find() method to find an object by a property value in an array of objects in JavaScript. The find() method returns the first element in the given array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

How do you compare elements in an array of objects?

Comparing item values #var isEqual = function (value, other) { // Get the value type var type = Object. prototype. toString. call(value); // If the two objects are not the same type, return false if (type !==


2 Answers

Using Array#filter, for this particular case the code would look like

var results = set.filter(function (entry) { return entry.color === "green"; }); 

Array#filter is not implemented in some older browsers, so see the linked article for a backward compatibility shim, or better yet get a full-fledged ES5 shim.

For the more general case, it's just a matter of extending this idea:

function findByMatchingProperties(set, properties) {     return set.filter(function (entry) {         return Object.keys(properties).every(function (key) {             return entry[key] === properties[key];         });     }); }  var results = findByMatchingProperties(set, { color: "green" }); 

Again, I am using ECMAScript 5 methods Object.keys and Array#every, so use an ES5 shim. (The code is doable without an ES5 shim but uses manual loops and is much less fun to write and read.)

like image 76
Domenic Avatar answered Oct 06 '22 15:10

Domenic


I have used map function from jquery and I am getting selected index by passing searched key value so by using that index we will get required object from array.

var mydata = [{ name: "Ram", Id: 1 }, { name: "Shyam", Id: 2 }, { name: "Akhil", Id: 3 }];  searchKey = 2 

var mydata = [{ name: "Ram", Id: 1 }, { name: "Shyam", Id: 2 }, { name: "Akhil", Id: 3 }];    searchKey = 2    var selectedData = mydata[mydata.map(function (item) { return item.Id; }).indexOf(searchKey)];    console.log(selectedData)
var selectedData = mydata[mydata.map(function (item) { return item.Id; }).indexOf(searchKey)];  console.log(selectedData)  output { name: "Shyam", Id: 2 }  Note: if you want to pass search key as object then searchKey = { Id: 2 };  mydata[mydata.map(function (item) { return item.Id; }).indexOf(searchKey.Id)];  output { name: "Shyam", Id: 2 } 
like image 28
Rambabu Bommisetti Avatar answered Oct 06 '22 16:10

Rambabu Bommisetti