How to find an appropriate object in array by one of its properties
The easiest way is to define a find function which takes a predicate
function find(arr, predicate) {
for (var i = 0; i < arr.length; i++) {
if (predicate(arr[i]) {
return arr[i];
}
}
return null;
}
Then you can just use this method on the array
var found = find(x, function (item) { item.id === 1002 });
A simple loop will suffice:
for(var i=0, len=x.length; i < len; i++){
if( x[i].id === 1002){
//do something with x[i]
}
}
Even though you are dealing with json remember that it's all just a hierarchy of arrays, objects and primitives.