我必须访问看起来像逗号分隔的数组列表的元素?
[
["service_state", "service_description", "service_icons", "svc_plugin_output", "svc_state_age", "svc_check_age", "perfometer"],
["OK", "Check_MK", "", "OK- Agent version 1.2.4p4, execution time 0.1 sec", "2017-05-01 21:11:33", "13 sec", "0.1s"]
]
实际字符串包含两个以上的数组,我不知道如何使用javascript访问每个元素。
非常感谢任何帮助。
答案 0 :(得分:0)
要访问多维数组,只需执行
array[0][0]
表示第一个子数组的第一个元素
在3个级别上它将是相同的
array = [ [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], 7 ];
array[0][1][1]
将提供5
答案 1 :(得分:0)
请参阅:https://jsfiddle.net/w2amcpbq/或以下代码段:
var multiArr = [
[
"service_state",
"service_description",
"service_icons",
"svc_plugin_output",
"svc_state_age",
"svc_check_age",
"perfometer"
],
[
"OK",
"Check_MK",
"",
"OK - Agent version 1.2.4p4, execution time 0.1 sec",
"2017-05-01 21:11:33",
"13 sec",
"0.1s"
]
];
var i = 0,
arrLength = multiArr.length;
for (i; i < arrLength; i++) {
var nestedArr = multiArr[i];
console.log(nestedArr);
for (var j = 0; j < nestedArr.length; j++) {
console.log(nestedArr[j]);
}
}
&#13;
答案 2 :(得分:0)
假设您要遍历所有元素,并且最初您有一个字符串。
var text = '[["service_state","service_description","service_icons","svc_plugin_output","svc_state_age","svc_check_age","perfometer"],["OK","Check_MK","","OK - Agent version 1.2.4p4, execution time 0.1 sec","2017-05-01 21:11:33","13 sec","0.1s"]]';
var array = JSON.parse(text);
array.forEach(function(nested) {
nested.forEach(function(el) {
console.log(el);
});
});
&#13;
答案 3 :(得分:0)
这应该可以解决问题:
for each (arr in multiArr) {
for each (v in arr) {
print(v)
}
}
答案 4 :(得分:0)
这是一个多维数组。将其视为一组行和列。您可以访问以下单个元素:array [row] [column]。
例如:
var arr = [[0,1,2],[3,4,5]];
arr[0][0] //this results in 0
arr[0][1] //this results in 1
arr[1][0] //this results in 3
遵循此逻辑,您可以使用嵌套for循环(循环内循环)循环遍历数组。
for(var i = 0; i < arr.length; i++) { // outer loop for rows
for(var j = 0; j < arr.length; j++) { // inner loop for columns
console.log(arr[i][j])
}
}