简体   繁体   English

如何获取此特定数据?

[英]How do I fetch this specific data?

I'm fetching data from this specific data:我正在从这个特定数据中获取数据:

{"cookie-stats": {"eating": 2, "5-minutes-ago": "-2"} {“cookie-stats”:{“吃”:2,“5 分钟前”:“-2”}

I'm currently doing this:我目前正在这样做:

{cookies['cookie-stats'].5-minutes-ago}

However, I keep getting the error: Syntax error: Unexpected token, expected "}"但是,我不断收到错误消息:语法错误:意外的令牌,预期的“}”

How do I get the value of "5-minutes-ago" when I'm fetching data?获取数据时如何获取“5-minutes-ago”的值?

To take the value of such a crazy prop that contains dash and numbers, you must use square brackets.要获取包含破折号和数字的这种疯狂道具的值,您必须使用方括号。

You should do something like that.你应该做这样的事情。

 let res = {"cookie-stats": {"eating": 2, "5-minutes-ago": "-2"}} console.log(res['cookie-stats']['5-minutes-ago'])

You could also do this:你也可以这样做:

const cookies = {
    "cookies": {
      "eating": 2,
      "fiveMinutesBefore": "-2",
  }
}

console.log(cookies.cookies.fiveMinutesBefore);

If you just change your naming convention it would solve your issue.如果您只是更改命名约定,它将解决您的问题。 The answer above is great too, but if you're looking to play around with dot notation this is the best way IMO.上面的答案也很好,但如果你想玩点符号,这是 IMO 的最佳方式。 The issue was that when you were trying to use.5-minutes-before it was not reading that key as a string, but instead the 5 was read as a Number.问题是,当您尝试使用.5-minutes-before 时,它不会将该键读取为字符串,而是将 5 读取为数字。

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

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