简体   繁体   English

如何访问 Arrays 中的嵌套数据和使用 Javascript 的对象

[英]How to access nested data in Arrays and Objects with Javascript

I am tring to use react-charts and the object they give as example data looks like this.我正在尝试使用 react-charts 和他们给出的 object 作为示例数据看起来像这样。

chartData: [
  {
    label: 'Series 1',
    data: [
      [0, 1],
      [1, 2],
      [2, 4],
      [3, 2],
      [4, 7],
    ],
  },
],

I want to build my own "data" and replace this temporary data but the whole object of arrays in objects in arrays (Or whatever it is confuses me.我想建立我自己的“数据”并替换这个临时数据,但是 arrays 的整个 object 在 arrays 的对象中(或者任何它让我感到困惑。

Can someone explain the nesting here.有人可以在这里解释嵌套。 Is it an array of 2 objects label and data and data` is an array of key value pairs.它是 2 个对象的数组labeldata and data` 是一个键值对数组。 Is that accurate?那准确吗?

I'm kind of trying something like this...我有点尝试这样的事情......

   let myData = []
   res.data.payload.forEach(function (item, index) {
      console.log(item, index)
      myData[(index, item.odds)]
   })
   this.setState({ chartData[data]: myData })

Am I even close?我什至接近吗?

Thanks谢谢

You can do like this你可以这样做

   let myData = []
   res.data.payload.forEach(function (item, index) {
      console.log(item, index)
      myData.push([index, item.odds])
   })
   this.setState({ chartData: [{...this.state.chartData[0], data: myData}] })

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

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