javascript - Array not storing values in for loop -


I want to parse a JSN object and store values ​​in an array, then I will assign each array to a second array In order to create an array for loop, where there is every element in a2 a1 .

I am already getting json and parsing correctly.

  var a1 = []; Var a2 = []; For (i = 0; i  

console.log (a1) prints the right array, which changes for each recurrence of the loop. For example:

  ["2014-01-01", "John"] ["2014-01-02", "n"] ["2014-01-03", " Mike "<< Code>  

but with console.log (a2) print a2 previous a1 values For each index:

  [["2014-01-03", "Mike"], ["2014-01-03", "Mike"], ["2014-01- 03  

Code> In every index of a2 for the inside of "Look" was also tried. Loop () like:

  a2 [i] = a1;  

I want a nested (or 2-D) array, but each Element Why is the value?

What's going on here? Is there some javascript scoping rules? It works in other languages ​​thanks!

Every time you go around the loop, you are referenced by Modify existing array a1 and then a2

This means that you end up with several references to the same array.

You need to create a new array to push for each group of data.

move var a1 = []; Inside Loop


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -