gpt4 book ai didi

javascript - 嵌套数组 for 循环

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

我一直在试图找出如何循环嵌套数组。

我有一个名为 worldShapes 的数组,其中包含子数组。我想循环遍历父数组并从中获取所有子数组。

这是我的尝试:

//Nested array

worldShapes = [

[33,108,66,141,99,174,99,207,132,207,165,207,165,240],

[132,306,165,306,165,339,165,372,132,405,99,405,99,438,132,438,165,438],

[198,339,231,339,264,372,297,372,330,405,363,438,396,438],

[198,174,198,273,231,306,264,306],

[231,174,231,240,264,273,297,273],

[396,306,462,306,495,339,495,372,528,405,528,438,561,438,594,471],

[660,504,561,504,495,504]

];

//trying to loop trough each item in the child array

(function(){
var wShapes = worldShapes; //create a local variable
var wLen = wShapes.length; //store the length as a variable

for (var i = 0; i < wLen; i++) {
for (var j = 0; j < wShapes[i].length; j++){
console.log(wShapes[i][j]); //this is propably wrong, trying to access the current child item of the current parent array
}
}
})

最佳答案

只需将 (); 添加到代码的最后;-)

您只是忘记调用匿名函数

关于javascript - 嵌套数组 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19200494/

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