gpt4 book ai didi

javascript - 循环遍历多级数组

转载 作者:行者123 更新时间:2023-11-28 11:29:48 25 4
gpt4 key购买 nike

我有一个具有多个级别的数组,每个数组都是一个“天”,每天都有不同的事件(目录)目录是一个数组,其中包含当天将提供的食物。我遇到的问题是尝试显示目录​​中的数据;

(4) [Array(7), Array(7), Array(7), Array(7)]
0: Array(7)
0: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
1: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
2: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
3: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
4: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
5: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
6:
fecha: 2019
id: 1553410800
mes: 3
catalogo: Array(2)
0: {id: "1553929200", tipo: "food",…}
1: {id: "1553995800", tipo: "food" …}
1: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
2: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
3: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]

通过这样做,我可以循环到子数组,如何循环/访问目录中的数据;

for ( var i = 0; i < data_array.length; i++ ) {
var childArray = data_array[i];
for( var j = 0; j < childArray.length; j++ ) {
}

我已经尝试过这个,但不起作用:

for ( var i = 0; i < data_array.length; i++ ) {
var childArray = data_array[i];
for( var j = 0; j < childArray.length; j++ ) {
var third_Array = childArray[j];
for ( var k = 0; k < third_Array.length; k++) {
console.log(third_Array);
}
}
}

最佳答案

没有实际数据很难测试,但试试这个:

const data = [Array(7), Array(7), Array(7), Array(7)]; // your data
data.forEach(day => {
day.forEach(element => {
element.catalogo.forEach(c => { console.log(c); }))
}
})

关于javascript - 循环遍历多级数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55363374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com