【问题标题】:How to access object property that has a date format?如何访问具有日期格式的对象属性?
【发布时间】:2019-04-09 13:41:57
【问题描述】:

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

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

【问题讨论】:

  • 不应将2018-09-07 用引号括起来""["2018-09-07"]
  • 你能把收到的json发回来吗?
  • @Farah10 “无法读取 '2018-09-07' 的属性 undefined。这意味着链在this.props.asteroids 之前都可以,但near_earth_objects 不是this.props.asteroids 的属性。能否请您发布数据
  • 未定义的原因可能有很多。例如,您可能正在尝试在它包含任何数据之前使用它。
  • @MaheerAli 我很笨。我试过了,然后又回来了“无法读取未定义的属性“2018-09-07””,所以认为这不是解决方案。结果我不得不把它放在引号中并做一个三元组以确保在运行 API 调用之前我没有显示数据。当然,我在发布这个问题时就弄清楚了。谢谢!

标签: javascript reactjs


【解决方案1】:

我检查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);
    }
});

【讨论】:

    【解决方案2】:

    你需要通过 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 2017-01-25
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多