Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  161] [ 1]  / answers: 1 / hits: 39322  / 8 Years ago, wed, august 17, 2016, 12:00:00

I have an array of object that looks like this:


var result = [{"id":"1", "price":"20.46"}, {"id":"2", "price":"40.00"}]

Right now I can access it like this:


result[0].price 

But what I am trying to do is loop through the array of objects and compare the id to a user inputted id and return the matching value. So the index should be irrelevant.


I tried to loop through the array of objects, but I probably made some mistake and got nothing back.


var userinputid = 1;

result.forEach(function() {
if (userinputid == result.id) {
alert(result.price);
}
);

How to solve this?


More From » jquery

 Answers
29

Instead of result.id you should use currentElementInLoop.id





var result = [{id:1,price:20.46},{id:2,price:40.00}]
var userinputid = 1;

result.forEach(function(e) {
if (userinputid == e.id) alert(e.price);
});




[#61002] Monday, August 15, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
whitney

Total Points: 642
Total Questions: 110
Total Answers: 98

Location: Solomon Islands
Member since Mon, Jun 20, 2022
2 Years ago
;