在javascript中循环遍历数组数组
我有一个如下所示的数组:在javascript中循环遍历数组数组,javascript,arrays,loops,Javascript,Arrays,Loops,我有一个如下所示的数组: var arrayElements= [ ["1","description 1", [ ["1.1","description 1.1"], ["1.2","description 1.2"], ["1.2","description 1.3"] ] ] , ["
var arrayElements=
[
["1","description 1",
[
["1.1","description 1.1"],
["1.2","description 1.2"],
["1.2","description 1.3"]
]
]
,
["2","description 2"],
["3","description 3",
[
["3.1","description 3.1"],
["3.2","description 3.2",
[
["3.2.1","description 3.2.1"],
["3.2.2","description 3.2.2"],
["3.2.2","description 3.2.3"]
]
],
["3.3","description 3.3"]
]
],
["4","description 4"]
];
description 1, depth 1
description 1.1, depth 2
description 1.2, depth 2
description 1.3, depth 2
description 2, depth 1
description 3, depth 1
description 3.1, depth 2
description 3.2, depth 2
description 3.2.1, depth 3
description 3.2.2, depth 3
description 3.2.3, depth 3
description 3.3, depth 2
description 4, depth 1
我希望通过以下数组循环,能够访问所有值以及每个元素的打印深度。控制台中的预期结果如下所示:
var arrayElements=
[
["1","description 1",
[
["1.1","description 1.1"],
["1.2","description 1.2"],
["1.2","description 1.3"]
]
]
,
["2","description 2"],
["3","description 3",
[
["3.1","description 3.1"],
["3.2","description 3.2",
[
["3.2.1","description 3.2.1"],
["3.2.2","description 3.2.2"],
["3.2.2","description 3.2.3"]
]
],
["3.3","description 3.3"]
]
],
["4","description 4"]
];
description 1, depth 1
description 1.1, depth 2
description 1.2, depth 2
description 1.3, depth 2
description 2, depth 1
description 3, depth 1
description 3.1, depth 2
description 3.2, depth 2
description 3.2.1, depth 3
description 3.2.2, depth 3
description 3.2.3, depth 3
description 3.3, depth 2
description 4, depth 1
以下是我尝试过的:
var depth = 0;
function listNodes(array){
for(i = 0; i < array.length;i++){
if( /* itearating over root array */ ){
depth = 1;
}
if(array[i][2] instanceof Array){
depth++;
console.log(array[i][1] + " depth: "+depth);
listNodes(array[i][2]);
}else{
console.log(array[i][1] + " depth: "+depth);
}
}
}
listNodes(arrayElements);
var深度=0;
函数listNodes(数组){
对于(i=0;i
我无法循环这个数组,它会被卡住,即使它应该继续。另一个问题是,我的变量存储深度不起作用,因为我无法确定循环何时在根数组上迭代,以便我可以重置depth
计数器。有什么想法吗?
var数组元素=
[
[“1”、“说明1”,
[
[“1.1”、“说明1.1”],
[“1.2”、“说明1.2”],
[“1.2”、“说明1.3”]
]
]
,
[“2”、“说明2”],
[“3”、“说明3”,
[
[“3.1”、“说明3.1”],
[“3.2”、“说明3.2”,
[
[“3.2.1”、“说明3.2.1”],
[“3.2.2”、“说明3.2.2”],
[“3.2.2”、“说明3.2.3”]
]
],
[“3.3”、“说明3.3”]
]
],
[“4”、“说明4”]
];
函数列表节点(数组、深度){
深度=深度的类型===“未定义”?1:深度;
var len=array.length;
for(var i=0;i 列表节点(数组元素)代码>您可以这样做
function listNodes(array, depth) {
depth++;
for (let i = 0; i < array.length; i++) {
console.log(array[i][1] + ' - ' + depth);
if (typeof array[i][2] === 'object') {
listNodes(array[i][2], depth);
}
}
}
listNodes(arrayElements, 0);
如果您可以使用对象而不是记住索引,则会更美观、更灵活:
var数组元素=[{
id:“1”,
说明:“说明1”,
儿童:[{
id:“1.1”,
说明:“说明1.1”,
儿童:[]
}]
},
{
id:“2”,
说明:“说明2”,
儿童:[{
id:“2.1”,
说明:“说明2.1”,
儿童:[{
id:“2.1.1”,
说明:“说明2.1.1”,
儿童:[]
},
{
id:“2.1.2”,
说明:“说明2.1.2”,
儿童:[]
}
]
}]
}
];
功能深度搜索(arr、深度、cb){
如果(深度===void 0){
深度=0;
}
如果(cb==无效0){
cb=功能(val){
控制台日志(val);
};
}
对于(变量i=0;i0){
深度搜索(val.children,深度+1,cb);
}
}
}
深度搜索(阵列)代码>depth
必须是函数参数,而不是共享变量。谢谢。添加参数是我一直在寻找的解决方案,除了将深度计数器的值存储在变量中之外,我并没有想出一个重置深度计数器的方法。