如何获取嵌套在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"]
}
];
/ ** /
如果我想获得喜欢&的价值“firstName”:“Akira”,我该怎么办? 有人可以帮忙吗?
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(); ?
}
1
答案 0 :(得分:0)
contacts
数组中的对象没有名称为prop
的属性。幸运的是,您可以通过名称访问对象的属性,类似于从数组中获取值的方式,因此如果使用contacts[i][prop]
,它将获得属性的值,该属性的名称是变量的值prop
,在本例中为"likes"
。由于likes
也是一个数组,因此您可以通过索引与contacts
数组索引相同的方式从中获取值。
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 likes array and loop through to print the values
// you might want to put this where a = true; is if you only want to get
// the likes where the firstName matches
var likes = contacts[i][prop];
for (var j = 0; j < likes.length; j++) {
console.log("contacts[" + i + "]." + prop + "[" + j + "] = " + likes[j]);
}
}
答案 1 :(得分:0)
Yoo可能如下所示;
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"]
}
],
getMeObj = (d,p,v,t) => d.filter(o => o[p] === v)[0][t];
console.log(getMeObj(contacts,"firstName", "Akira", "likes"));
&#13;
答案 2 :(得分:0)
以下代码可以使用
var firstName = "Akira", prop = "likes";
for(var i in contacts)
{
if(contacts[i].firstName == firstName){
var likes = contacts[i][prop];
console.log(likes);
}
答案 3 :(得分:0)
我修复了你的代码。如果你需要更多细节问我,但我认为代码很简单
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;
}
for (var n = 0; n < contacts[i].likes.length; n++) {
console.log("contacts[" + i + "].likes[" + n + "] = " + contacts[i].likes[n]);
}
}
作为旁注:答案应与提问者处于同一级别以帮助学习:)