简体   繁体   English

当元素名称为日期时,如何访问 JSON object 元素?

[英]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.我正在为 MMO 开发损益跟踪器,它使用游戏 API 来获取有关游戏经济中某些项目的数据。

It returns this data as a JSON object, I am attempting to use this data to populate graphs in future.它以 JSON object 的形式返回此数据,我正尝试在将来使用此数据来填充图表。 I'm struggling to access each element of the object. The data returned looks like so:我正在努力访问 object 的每个元素。返回的数据如下所示:

 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?我需要访问每个元素,然后使用它的名称和值来填充图形,我 go 如何执行此操作?

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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM