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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using
Array#filter, for this particular case the code would look likeArray#filteris 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:
Again, I am using ECMAScript 5 methods
Object.keysandArray#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.)