Sam Saint-Pettersen
Sam Saint-Pettersen

Reputation: 511

Append to an array within a specific index

I've been really stumped for a while on this. I have a loop like this:

for(var i = 0; i < Quotes.length; i++) {
    var timestamp = new Date().getTime();
    dataSets.push([[ timestamp, Quotes[i].unitprice ]]);
}

dataSets should be an array of timestamp and price data (Quotes[i].unitprice). Timestamp and price data is also an array which should be appended to for each stock; every update cycle when this loop is called.

How can I append to an array contained within a specific dataSets index.

Currently dataSets is defined in global scope as dataSets = [[]].

I just want to be able to update the inner array on each index (dataSets[i]).

Any help is much appreciated. Thank you.

EDIT: Say I have the stock quotes FOO and BAR; I want to be able to append to their index within dataSets. So where dataSets[0] is FOO and dataSets[1] is BAR, I want to be able to update the timestamp and unit price array from [[134484, 1.40]] to [[134484, 1.40],[134487, 1.45]]

Upvotes: 0

Views: 58

Answers (1)

Krzysztof Kosiński
Krzysztof Kosiński

Reputation: 4325

Probably you are looking for something like this. First create all the empty arrays within dataSets:

var dataSets = new Array();
for (var i = 0; i < Quotes.length; ++i) {
    dataSets[i] = new Array();
}

Then append the data with:

dataSets[i].push([timestamp, Quotes[i].unitprice]);

Upvotes: 1

Related Questions