in my javascript I have this array
var versions =
[{"id":"454","name":"jack"},
{"id":"4","name":"rose"}
{"id":"6","name":"ikma"}
{"id":"5","name":"naki"}
{"id":"667","name":"da开发者_如何学JAVAsi"}
]
I want to parse it for the name where id is 4. how would i do that.
How about:
$.each(versions, function(index, value) {
if (value['id'] === '4') {
alert('got it!');
}
});
This is untested, but should work.
for(var i in versions) {
if(versions[i].id == 4) {
alert(versions[i].name);
break;
}
}
Edit: Added short circuit.
You'll have to iterate through the array and pull out the names where the id equals "4"
var names = [];
for(var i=0; i<versions.length; i=i+1)
{
var version = versions[i];
if(version.id == "4")
{
names.push(version.name);
}
}
var versions = {
'454': {name: 'jack'},
'4': {name: 'rose'},
'6': {name: 'ikma'},
'5': {name: 'naki'},
'667': {name: 'dasi'}
};
alert(versions['4'].name);
精彩评论