[英]Trouble accessing values of array in javascript
我有一个从后端接收的JavaScript数组,其结构如下:
scope.myArray = [{"0":[1,2,3,4],"1":[0,2,3,1,4]},{"0":[1,2,3,4],"1":[0,2,3,1,4]}]
我试图通过执行以下操作来检索某些值:
for(var x=0;x<scope.myArray.length;x++){
console.log("myArray is ....... " + JSON.stringify(scope.myArray[x].0))
}
和这个
for(var x=0;x<scope.myArray.length;x++){
console.log("myArray is ....... " + JSON.stringify(scope.myArray[x]."0"))
}
但都行不通。
任何帮助,将不胜感激。 提前致谢。
你这样的括号符号
var myArray = [{"0":[1,2,3,4],"1":[0,2,3,1,4]},{"0":[1,2,3,4],"1":[0,2,3,1,4]}] for (var x = 0; x < myArray.length; x++) { console.log("myArray is ....... " + JSON.stringify(myArray[x][0])); }
您应该通过方括号符号访问属性。
for (var x=0; x < scope.myArray.length; x++){
console.log("myArray is ....... " + JSON.stringify(scope.myArray[x]['0']))
}
您创建了一个包含一个对象的数组,其键为“ 0”,“ 1”等。
尝试一下:
scope.myArray = [
{
"0": [1, 2, 3, 4],
"1": [0, 2, 3, 1, 4]
},
{
"3": [1, 2, 3, 4],
"4": [0, 2, 3, 1, 4]
}
];
scope.myArray.forEach(function(item){
console.log("myArray is ....... " + JSON.stringify(item[0]))
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.