2013-02-08 4 views
0

Quelqu'un pourrait-il s'il vous plaît me aider à faire ce qui suit avec JavaScript:JS - remplir un tableau d'un autre tableau + modifier les valeurs

  • J'ai un tableau (matrice1) qui contient des chaînes
  • je dois boucle à travers chaque élément du tableau initial (array1)
  • puis j'ai besoin d'obtenir la valeur de chaque indice dans array1, et ajouter une lettre à chaque valeur
  • et enfin d'écrire la valeur modifiée de chaque indice de array1 dans array2.

Merci.

+3

[Qu'avez-vous essayé?] (Http://whathaveyoutried.com/) – ruakh

+0

Array.map est un bon début – rlemon

Répondre

1
//declares the array and initializes it with strings 
var array1 = ["one", "two", "three", "four"]; 

//declares the second array 
var array2 = []; 

//this line begins the loop. Everything inside the { } will run once for every single item inside array1 
for (var i = 0; i < array1.length; i++) 
{ 
    //this gets the contents of the array at each interval 
    var string = array1[i]; 

    //here we take the original string from the array1 and add a letter to it. 
    var combo = string + "A"; 

    //this line takes the new string and puts it into the 2nd array 
    array2.push(combo); 
} 

//displays a message box that shows the contents of the 2nd array 
alert(array2); 
+0

merci beaucoup. – MousseDeHumus

Questions connexes