首页 > 解决方案 > 如何访问具有日期格式的对象属性?

问题描述

我需要从嵌套对象中的 NASA API 访问一些数据。他们的数据按日期排序,因此每个键的格式如下:2018-09-07

const asteroidList = this.props.asteroids.near_earth_objects //works fine, can access data up to this point
console.log(asteroidList)
const asteroidList = this.props.asteroids.near_earth_objects[2018-09-07] // "Legacy octo literals are not allowed in strict mode" ????
console.log(asteroidList) //errors out

在这一点之后,我无法访问任何内容,因为我的文本编辑器不断收到错误消息。我假设必须有某种转换方法或某种东西来读取我不知道的日期,但我找不到对象键值对的任何东西。

标签: javascriptreactjs

解决方案


我检查NASA APIs响应是什么,关键不是a date-value,而是"date"字符串。

因此,如果您有一个对象列表,并且只想访问具有特定日期的对象,则必须遍历这些对象并搜索该特定对象:

const list = [
    {
        "date": "1995-06-16",
        "explanation": "Today's Picture:    Explanation:  If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star.   We keep an  archive file.  Astronomy Picture of the Day is brought to you by  Robert Nemiroff and  Jerry Bonnell . Original material on this page is copyrighted to Robert Nemiroff and Jerry Bonnell.",
        "hdurl": "https://apod.nasa.gov/apod/image/e_lens.gif",
        "media_type": "image",
        "service_version": "v1",
        "title": "Neutron Star Earth",
        "url": "https://apod.nasa.gov/apod/image/e_lens.gif"
    },
    {
        "date": "1999-07-11",
        "explanation": "Today's Picture:    Explanation:  If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star.   We keep an  archive file.  Astronomy Picture of the Day is brought to you by  Robert Nemiroff and  Jerry Bonnell . Original material on this page is copyrighted to Robert Nemiroff and Jerry Bonnell.",
        "hdurl": "https://apod.nasa.gov/apod/image/e_lens.gif",
        "media_type": "image",
        "service_version": "v1",
        "title": "Neutron Star Earth",
        "url": "https://apod.nasa.gov/apod/image/e_lens.gif"
    },
    {
        "date": "2010-01-22",
        "explanation": "Today's Picture:    Explanation:  If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star.   We keep an  archive file.  Astronomy Picture of the Day is brought to you by  Robert Nemiroff and  Jerry Bonnell . Original material on this page is copyrighted to Robert Nemiroff and Jerry Bonnell.",
        "hdurl": "https://apod.nasa.gov/apod/image/e_lens.gif",
        "media_type": "image",
        "service_version": "v1",
        "title": "Neutron Star Earth",
        "url": "https://apod.nasa.gov/apod/image/e_lens.gif"
    }
]

list.forEach(element => {
    if (element.date === '1999-07-11') {
        console.log(element);
    }
});


推荐阅读