2013-10-05 4 views
0

중첩 배열을 반복하는 방법을 알아 내려고했습니다.루프 중첩 배열

하위 배열이 포함 된 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 
      } 
     } 
    }) 
+0

예상되는 결과는 무엇입니까? – thefourtheye

+0

코드에 어떤 문제가 있습니까? 실행 해 보셨습니까? – leesei

+1

함수에 래핑했지만 함수를 실행하지 않습니다. 하지만 왜 함수로 묶어야합니까? –

답변

4

를 추가 그냥 코드의 끝까지 ();를 추가 ;-)

당신은 단순히 당신의 전화를 잊어 버렸습니다. 익명 함수

+0

당신이 맞습니다 : D 이것은 당황 스럽습니다! 고맙습니다! –

3

이 기능을 실행하려면, ()

(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 

Fiddle