我有这个数组:
{"endroits":[{"id":"8","pseudo":"khaled","password":"android321"},
{"id":"9","pseudo":"oussama","password":"laidi"},
{"id":"11","pseudo":"Moulay","password":"fitna"},
{"id":"13","pseudo":"admin","password":"admin"},
{"id":"14","pseudo":"laidi","password":"l3ide"},
{"id":"15","pseudo":"tarabet","password":"tarabet"}]}
我想浏览并获取属性(id,伪,密码)。我怎么能这样做?
答案 0 :(得分:0)
您可以按照以下方式执行此操作
var obj = {"endroits":[{"id":"8","pseudo":"khaled","password":"android321"},{"id":"9","pseudo":"oussama","password":"laidi"},{"id":"11","pseudo":"Moulay","password":"fitna"},{"id":"13","pseudo":"admin","password":"admin"},{"id":"14","pseudo":"laidi","password":"l3ide"},{"id":"15","pseudo":"tarabet","password":"tarabet"}] };
alert( obj.endroits[0].id );
alert( obj.endroits[0].pseudo );
编辑(为所有人)
for (elem in obj.endroits) {
console.log(obj.endroits[elem].id);
console.log(obj.endroits[elem].pseudo);
console.log(obj.endroits[elem].password);
}
OR
for(var i=0;i<obj.length;i++)
{
alert( obj.endroits[i].id );
alert( obj.endroits[i].pseudo );
}
答案 1 :(得分:0)
尝试使用Array.prototype.forEach()
var data = {
"endroits": [{
"id": "8",
"pseudo": "khaled",
"password": "android321"
}, {
"id": "9",
"pseudo": "oussama",
"password": "laidi"
}, {
"id": "11",
"pseudo": "Moulay",
"password": "fitna"
}, {
"id": "13",
"pseudo": "admin",
"password": "admin"
}, {
"id": "14",
"pseudo": "laidi",
"password": "l3ide"
}, {
"id": "15",
"pseudo": "tarabet",
"password": "tarabet"
}]
}
data.endroits.forEach(function(val) {
console.log(val.id, val.pseudo, val.password)
})
答案 2 :(得分:0)
这是一个使用jQuery.each
的plunkerhttps://plnkr.co/edit/L8NOAJZkWQTDqXVIcQQ9
var obj = {
"endroits": [{
"id": "8",
"pseudo": "khaled",
"password": "android321"
}, {
"id": "9",
"pseudo": "oussama",
"password": "laidi"
}, {
"id": "11",
"pseudo": "Moulay",
"password": "fitna"
}, {
"id": "13",
"pseudo": "admin",
"password": "admin"
}, {
"id": "14",
"pseudo": "laidi",
"password": "l3ide"
}, {
"id": "15",
"pseudo": "tarabet",
"password": "tarabet"
}]
};
document.write(obj["endroits"][0].pseudo);
document.write("<br>");
document.write(obj["endroits"][1].pseudo);
document.write("<br><br>");
document.write("jQuery Foreach:<br>");
jQuery.each(obj["endroits"], function(index, value) {
document.write(value.pseudo)
document.write("<br>");
})