访问带有react的json多维数组

时间:2018-04-04 15:47:11

标签: javascript json reactjs

我正在尝试访问我的JSON的某个元素,通过React JS我在json上使用:Object.keys(field)[0]并得到“punteggio”但我希望得到它的值,例如“7 ”。我如何访问这些数据?

  "honours": [
  {
      "punteggio" : 7,
      "serie" : "Serie A",
      "tipo" : "icon-scudetto",
      "anni" : "1926/1927 revocato, 1027/1928, 1942/1943, 1945/1946, 1947/1948"
  },
  {
      "punteggio" : 4,
      "serie" : "Serie B",
      "tipo" : "icon-scudetto",
      "anni" : "1958/1960, 1989/1990, 2000/2001, 2011/2012"
  },
  {
      "punteggio" : 5,
      "serie" : "Serie B",
      "tipo" : "icon-coppaitalia",
      "anni" : "1958/1960, 1989/1990, 2000/2001, 2011/2012"
  }
]

2 个答案:

答案 0 :(得分:0)

“punteggio” - 只是一个键,所以你可以像这样访问它的值

const key = Object.keys(field)[0];
const value = field[key];

答案 1 :(得分:0)

当您需要从对象

获取数组键时使用的Object.Keys

使用Object.Values获取值

了解详情: https://javascript.info/keys-values-entries