Answer the question
In order to leave comments, you need to log in
How to get the index of an object in JSON by its id?
The situation is this, I have a json file with the following structure:
[
{
....
"id": 1,
...
},
{
....
"id": 2,
...
},
...
]
var id = 2;
data[1]
Answer the question
In order to leave comments, you need to log in
function getIndexById(arr, id) {
var index = -1, length = arr.length, i = 0;
for (; i < length; i++) {
if (arr[i]['id'] === id) {
index = i;
break;
}
}
return index;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question