0

When I am trying to access the 0th index of the data I am getting error undefined but when I am trying to print out the whole object it is working fine. I am unable to access any element or loop over the data. The data variable works fine but the tSeries variable is undefined.

This is the code snippet:

var data;
var tSeries;



(async () => {
    const response = await fetch("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=" + key);
    apidata = await response.json();
    data = (apidata["Meta Data"]["2. Symbol"]);
    tSeries = (apidata["Time Series (Daily)"][0]);
    console.log(tSeries);
    
})();

Here is the JSON response snippet:

{
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "IBM",
        "3. Last Refreshed": "2020-06-24 16:00:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2020-06-24 16:00:00": {
            "1. open": "116.4600",
            "2. high": "116.6700",
            "3. low": "116.3800",
            "4. close": "116.4900",
            "5. volume": "263785"
        },
        "2020-06-24 15:55:00": {
            "1. open": "116.8000",
            "2. high": "116.8600",
            "3. low": "116.3500",
            "4. close": "116.4700",
            "5. volume": "154646"
        },
        "2020-06-24 15:50:00": {
            "1. open": "117.0100",
            "2. high": "117.1400",
            "3. low": "116.8500",
            "4. close": "116.8500",
            "5. volume": "99465"
        },
        "2020-06-24 15:45:00": {
            "1. open": "116.6900",
            "2. high": "117.0400",
            "3. low": "116.6600",
            "4. close": "117.0100",
            "5. volume": "83736"
        }
indianLeo
  • 105
  • 1
  • 5
  • `Time Series (Daily)` seems to be an object without any purely numeric keys. Accessing objects by an index has been last possible for two decades ago. – Teemu Jun 25 '20 at 09:32
  • 1
    If the keys are dynamic, loop through them: `const daily = apidata["Time Series (Daily)"]; for(const key in daily){ console.log(key, daily[key]) }` If you want the first key, then `Object.keys(daily)[0]`, first entry: `Object.entries(daily)[0]` – adiga Jun 25 '20 at 09:46
  • Can you please explain as why did it not work? – indianLeo Jun 25 '20 at 11:25

2 Answers2

0

It won't work like that.

You can either do apidata["Time Series (Daily)"]["2020-02-03"]

to get a specific object for the date, or you could use map/reduce/filter in order to get the desired results.

Object.values(apidata["Time Series (Daily)"]).map((value, index) => console.log(value));

Or for keys:


Object.keys(apidata["Time Series (Daily)"]).map((key, index) => console.log(key));

But you can do whatever you want with the values that you get from there.

mutantkeyboard
  • 1,289
  • 15
  • 36
-1

apidata["Time Series (Daily)"]["2020-06-24 16:00:00"]
should work.

Or you can put these daily objects on the array and then pick the first one.

Rohit Ambre
  • 628
  • 1
  • 5
  • 18
jatu
  • 1