codenoob codenoob - 1 year ago 89
Javascript Question

jquery - get value from array object

I have an array of object that look like this

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

right now I can access it by


but what I am trying to do is loop through the array of object, and compare the id to a user inputed id and return the matching value. So the index should be irrelevant

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

var userinputid = 1;

if(userinputid =={

How to solve this.

Answer Source

Instead of you should use

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download