kylas
kylas

Reputation: 1455

Unable to print multidimensional array

Below is my javascript code:

var newData =[];
for(var j = 0; j<dates.length;j++){
    newData = [[dates[j],close[j]]];
}
document.write(newData[1]);

When I tried to print newData[2] and onwards, it shows undefined. Only newData[1] shows me the actual value. Is there anything wrong in the code above ? I am sure that dates and close array contains all the values needed

Upvotes: 0

Views: 39

Answers (2)

Kirbo
Kirbo

Reputation: 381

If you want to overwrite existing data of specific key, here is how you can do it:

for(var j = 0; j<dates.length;j++){
    newData[j] = [dates[j],close[j]];
}

If you want to create an array from the scratch, do as @suvroc suggested

Upvotes: 0

suvroc
suvroc

Reputation: 3062

You should push one array into outside array like this:

var newData =[];
for(var j = 0; j<dates.length;j++){
    newData.push([dates[j],close[j]]);
}

Otherwise you will overwrite you newData array in each loop

Upvotes: 4

Related Questions