2013-10-05 6 views
0

Je suis bloqué en essayant de comprendre comment faire une boucle dans les tableaux imbriqués.Tableau imbriqué pour la boucle

J'ai un tableau appelé worldShapes qui contient des tableaux enfants. Je veux faire une boucle dans le tableau parent et obtenir tous les tableaux enfants de celui-ci.

Voilà ma tentative:

//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

Quelle est la sortie attendue? – thefourtheye

+0

Quel est le problème avec votre code? Avez-vous essayé de l'exécuter? – leesei

+1

Vous l'avez enveloppé dans une fonction, mais vous n'exécutez pas la fonction. Mais pourquoi envelopper dans une fonction du tout? –

Répondre

4

ajouter juste (); à la fin de votre code ;-)

Vous avez simplement oublié d'invoquer votre fonction anonyme

+0

Vous avez raison: D C'est embarrassant! Je vous remercie! –

3

Pour exécuter la fonction, ajoutez ()

(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