如何访问具有日期格式的对象属性?

时间:2019-04-09 13:41:57

标签: javascript reactjs

我需要从嵌套对象中的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

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

2 个答案:

答案 0 :(得分:0)

我检查什么是NASA APIs响应,密钥不是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);
    }
});

答案 1 :(得分:-1)

您需要在引号"2018-09-07"中传递 2018-09-07 还要添加对未定义案例的检查,以避免错误。

const asteroidList = this.props.asteroids && this.props.asteroids.near_earth_objects && this.props.asteroids.near_earth_objects['2018-09-07']
console.log(asteroidList)