[英]How do I access properties of an object that's inside of an array that's inside of an array that's inside of an array?
我想做的是访问嵌套数组内部的对象信息。 就像是:
<html>
<body>
<p id="demo"></p>
<script>
var people=[[[]]];
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
people[0][0][0].push(person);
document.getElementbyId("demo").innerHTML=people[0][0][0]person.lastName;
</script>
</body>
</html>
我知道要访问数组的元素0,我会使用array [0]。 我知道要访问对象的属性,我将使用object.attribute。 但是我似乎无法弄清楚如何访问数组内部的对象属性。 有人可以帮我吗?
您的一个数组是示例代码的缩写。 您的代码导致
var people=[[[]]];
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
people[0][0][0].push(person);
返回:TypeError:无法读取未定义的属性“ push”
你需要改变
var people=[[[]]];
至
var people=[[[[]]]];
那你就可以做
people[0][0][0][0].firstName
返回:“约翰”
正确的代码段如下:
<html>
<head>
</head>
<body>
<p id="demo"></p>
<script>
var people=[[[]]];
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
people[0][0].push(person);
document.getElementById("demo").innerHTML = people[0][0][0]["firstName"];
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.