Skip to content
Advertisement

How to access JSON object element when the name of the element is a date?

I am developing a profit and loss tracker for an MMO, it uses the games API to get data about certain items in the games economy.

It returns this data as a JSON object, I am attempting to use this data to populate graphs in future. I’m struggling to access each element of the object. The data returned looks like so:

 daily: {2020-05-19T00:00:00.000Z: 794, 2020-05-20T00:00:00.000Z: 823, ... ETC

I need to access each element, then use its name and value to populate the graph, how would I go about doing this?

Advertisement

Answer

I need to access each element, then use its name and value to populate the graph

You can do that in many ways, such as:

for (let key of Object.Keys(yourObject)){
    let value = yourObject[key];
    //use both key and value as you please
}

If you need to access a specific property with a key like you have shown, you can do it with:

let value = yourObject["2020-05-19T00:00:00.000Z"];
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement