Javascript 循环的嵌套数组
我一直在想如何通过嵌套数组循环 我有一个名为worldShapes的数组,其中包含子数组。我想循环父数组并从中获取所有子数组 以下是我的尝试:Javascript 循环的嵌套数组,javascript,arrays,Javascript,Arrays,我一直在想如何通过嵌套数组循环 我有一个名为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,33
//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
}
}
})
//嵌套数组
世界形状=[
[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]
];
//尝试通过子数组中的每个项循环
(功能(){
var wShapes=worldShapes;//创建一个局部变量
var wLen=wShapes.length;//将长度存储为变量
对于(变量i=0;i
要执行该函数,请添加()
(函数(){
var wShapes=worldShapes;//创建一个局部变量
var wLen=wShapes.length;//将长度存储为变量
对于(变量i=0;i
只需添加()代码>到代码的最后;-)
您只是忘记调用匿名函数了预期的输出是什么?代码有什么问题?你试过运行它吗?你已经将它包装在一个函数中,但是你没有执行这个函数。但是为什么要把它包装在一个函数中呢?你收到了错误消息吗?你的代码看起来绝对正确。什么是你认为它不应该做的?
(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
}
}
}()); // here