javascript - find a value inside array of JSON object -
i below array of json objects jsp
"titles":[ { "book3" : "bulletin 3" } , { "book1" : "bulletin 1" } , { "book2" : "bulletin 2" } ]
on js side, parsed , see array 3 objects. now, want find/identify value when pass string key.
for e.g. when pass "book2" should value "bulletin 2". can me identify approach?
try this
var data = { "titles": [{ "book3": "bulletin 3" }, { "book1": "bulletin 1" }, { "book2": "bulletin 2" }] }; function getvaluebykey(key, data) { var i, len = data.length; (i = 0; < len; i++) { if (data[i] && data[i].hasownproperty(key)) { return data[i][key]; } } return -1; } console.log(getvaluebykey('book2', data.titles));
Comments
Post a Comment