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.
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.
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" });
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.
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 !==
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.)
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With