如何獲取嵌套在JSON值中的數組的值以及嵌套在數組中的JSON?我怎樣才能得到嵌套在JSON值和JSON嵌套在數組中的數組值?
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
/**/
,如果我想喜歡& 「名字」 的價值: 「晃」,我該怎麼辦? 有人可以幫忙嗎?
var firstName = "Akira", prop = "likes";
for (var i = 0; i < contacts.length; i++) {
var a;
console.log("contacts[i].firstName = "+contacts[i].firstName);
if(contacts[i].firstName == firstName){
a = true;
}else{
a = false;
}
//get the cales of likes?
console.log("contacts[i].prop[i].value() = "+contacts[i].prop[i].value());
//contacts[i].prop[i].value(); ?
}
問題是什麼?你的代碼似乎沒問題。你有'a = true'的地方,你可以去'contacts.likes'或'contacts [i] [prop]'來獲得數組。 – Shilly
https://gist.github.com/xgqfrms-GitHub/78fbe4ef38ee1e50965c1558a5b6245f – xgqfrms
https://github.com/gildata/RAIO/issues/61 – 2017-08-10 06:57:42