我有一个对象数组,我想选择该数组中某个对象的属性。当我尝试以下代码时,它不起作用,我在字符串中没有得到任何值:
var _string = teams[2].name;
数组代码如下:
var teams = new Array (team1, team 2, team3);
var team1 = {
name: "team 1",
matches: 5
}
var team2 = {
name: "team 2",
matches: 4
}
var team3 = {
name: "team 3",
matches: 3
}
一些帮助会很棒:-)
谢谢
G
最佳答案
您应该首先声明您的团队,然后声明您的数组,如下所示:
var team1 = {
name: "team 1",
matches: 5
}
var team2 = {
name: "team 2",
matches: 4
}
var team3 = {
name: "team 3",
matches: 3
}
var teams = [team1, team2, team3];
另请注意,代码中的 team
和 2
之间有一个空格,这是不正确的。
关于Javascript:从数组中的对象中选择属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29467340/