TVH7 TVH7 - 1 year ago 89
Javascript Question

How to get json object by javascript item

So I have an array of json objects like these below. Which are retrieved using XMLHTTP. Now I want to 'select' value where name == "test" using javascript how do I do this? I know I can do for example data[0] but that is not how I want is because the Id can change.


{"name" : "test", "value": "Something"},
{"name" : "test2", "value": "Something else"}


Answer Source

You can use loops for this, so you can use Array.filter and you can get the first match, ex.:

var items = [{"name" : "test", "value": "Something"},{"name" : "test2", "value": "Something else"}];

var where = "name", is = "test2"
, select = items.filter(function(item){ return item[where] == is})[0];


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