1

I am working on charts for stock data. I am using the Alpha Vantage API to get daily stock prices and dates. I require an array populated by arrays with two array elements at a time like this...[[01/25/2020, 34.10], [01/26/2020, 41.67]]. I currently have two arrays, one for the close dates and close prices respectively. How could I merge the two arrays so as to get a final array with the populated elements as I described?

index.js

var ticker = 'AAPL'

async function getMatchingStockPricingData(ticker) {
  const res = await fetch(`https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=${ticker}&apikey=dMVPYPPFWDTRLQU0S`)
  return await res.json()
}

async function createStockChart() {
  var matchingStockDailyPriceDataResults = await getMatchingStockPricingData(ticker)
  var matchingStockDailyPriceData = matchingStockDailyPriceDataResults["Time Series (Daily)"]

  var closePrices = []
  var closeDates = []

  for (var key in matchingStockDailyPriceData) {
    closePrices.push(parseFloat(matchingStockDailyPriceData[key]['4. close']));
    closeDates.push(key);
  }

  console.log(closePrices)
  console.log(closeDates)
}

createStockChart()

Screenshot:

Aaron3219
  • 2,168
  • 4
  • 11
  • 28

1 Answers1

1
let stockData = closeDates.map((date, index) => {
  return [date, closePrices[index]];
});
Max Braun
  • 27
  • 3