Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  195] [ 3]  / answers: 1 / hits: 28783  / 8 Years ago, wed, july 20, 2016, 12:00:00

I have this array of objects:



var frequencies = [{id:124,name:'qqq'}, 
{id:589,name:'www'},
{id:45,name:'eee'},
{id:567,name:'rrr'}];


I need to get an object from the array above by the id value.



For example I need to get the object with id = 45.



I tried this:



var t = frequencies.map(function (obj) { return obj.id==45; });


But I didn't get the desired object.



How can I implement it using JavaScript prototype functions?


More From » javascript

 Answers
58

You need filter() not map()



var t = frequencies.filter(function (obj) { 
return obj.id==45;
})[0];

[#61307] Sunday, July 17, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shantelc

Total Points: 737
Total Questions: 120
Total Answers: 104

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
;