1

I wrote some code where data is going into an array. At each index, I have another array that has an array with five data elements. What I want to do is to add data at the end of each nested array.

var allTimeStamps = [];
var allTimeStampsData = [];
$.getJSON( "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo", function( data ) {

    const responseData = data;

    for(let key in responseData["Time Series (Daily)"]){
        allTimeStamps.push(key);
        allTimeStamps.push(parseFloat((responseData["Time Series (Daily)"][key]["3. low"])));
        allTimeStamps.push(parseFloat(responseData["Time Series (Daily)"][key]["1. open"]));
        allTimeStamps.push(parseFloat(responseData["Time Series (Daily)"][key]["4. close"]));
        allTimeStamps.push(parseFloat(responseData["Time Series (Daily)"][key]["2. high"]));
        allTimeStampsData.push(allTimeStamps);
        allTimeStamps=[];
    }
    console.log("seperatedData", allTimeStampsData);                
});

I am trying something like this:

Old

allTimeStampsData[0].append("121");

New

allTimeStampsData[0].push("121");

I wrote append because of Python. I was actually trying to push, but this is not working.

I am expecting output like:

[ ["08-2-2018",98,12,98,78,121] ,......]
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
curious_nustian
  • 596
  • 2
  • 7
  • 22

4 Answers4

3

The best way to do something like this is with a simple .map over the entries of the "Time Series (Daily)" property, from which you can extract the date and the low/open/close/high:

fetch("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo")
  .then(res => res.json())
  .then(responseData => {
    const daily = responseData["Time Series (Daily)"];
    const keys = [
      '3. low',
      '1. open',
      '4. close',
      '2. high',
    ];
    const allTimeStampsData = Object.entries(daily)
      .map(([date, infoObj]) => (
        [date, ...keys.map(key => infoObj[key])]
      ));
    console.log(allTimeStampsData[0]);
    console.log(allTimeStampsData);
  });
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

I believe what you're looking for is the push method.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Initialize your array var my_arr = ['Volvo', 'Ford', 'Chevrolet'];

Then use the push method to add a new value to it.

my_arr.push('Mercedes');

Now my_arr = ['Volvo', 'Ford', 'Chevrolet', 'Mercedes'];

0

You should explicitly assign the field in your allTimeStampsData array:

allTimeStampsData[allTimeStampsData.length] = allTimeStamps;

Thereafter, allTimeStampsData[i].push("121"); will work.

collapsar
  • 17,010
  • 4
  • 35
  • 61
0

I think the original question needs to be clarified here folks. As @josh mentioned that you use $.push() to append data to the end of an array. Regardless of how many levels deep the Array is using .push() is the best way to add data to an array. Also this sounds like a duplicate question which is posted here javascript-push-multidimensional-array

Rylan
  • 36
  • 1
  • 1
    This feels more like a comment rather than an answer that will be useful now or in the future. If this is a duplicate, just flag or comment as such and mods will usually pick that thread up. –  Jul 06 '18 at 20:01