简体   繁体   English

从数组扩展现有对象的数据

[英]Extending data of an existing object from an array

I receive arrays from an API in a JSON format.我从 JSON 格式的 API 接收数组。 I want to merge them into the nested array called which consists of objects graphData .我想将它们合并到名为的嵌套数组中,该数组由对象graphData组成。 How do I extend the data object by one entry?如何通过一个条目扩展数据对象?

mounted() {

   var i = 0;
   const axios = require('axios');
    //Usage
    axios.get("https://3.11.40.69:9001/app/usageUnitForCustomer/1521/2019-12-30/2020-12-30")
      .then(response => {
        const time = response.data.event_time; //["2019-12-30T14:06:21.000Z", "2019-12-30T14:06:21.000Z"]
        const unit = response.data.unit; //[44.67, 75.89]
        for (i = 0; i < 1; i++) {
          if (time[i] !== time[i + 1]) {
            this.graphData[1].data.time[i] = unit[i];
          }
        } 
      })
    //Revenue
    axios.get("https://3.11.40.69:9001/app/revenueUnitForMachine/345/2019-12-30/2020-12-30")
      .then(response => {
        const time = response.data.event_time; //["2019-12-30T14:06:21.000Z", "2019-12-30T14:06:21.000Z"]
        const unit = response.data.revenue; //[44, 75]
        for (i = 0; i < 10; i++) {
          if (time[i] !== time[i + 1]) {
            this.graphData[0].data.time[i] = unit[i];
          }
        } 

      })
  },
  data() {
      return {
        graphData: [
          {name: 'Revenue', data: {}},
          {name: 'Usage', data: {}}
        ]
      }
    }

After executing the above code both data objects are still empty.执行上述代码后,两个数据对象仍然是空的。 The outcome looks like this and following error message:结果如下所示,并显示以下错误消息:

0:
name: "Revenue"
data: Object []

1:
name: "Usage"
data: Object []

Uncaught (in promise) TypeError: Cannot set property '0' of undefined

Expected outcome:预期结果:

graphData: [
          {name: 'Revenue', data: {'2019-12-30T14:06:21.000Z': 448, '2019-12-30T14:06:22.000Z': 44}},
          {name: 'Usage', data: {'2019-12-30T14:06:21.000Z': 448, '2019-12-30T14:06:22.000Z': 44}}
        ]

Has anyone an idea?有人有想法吗?

You cannot reach outside variables from within a .then() block.您无法从 .then() 块内访问外部变量。 You should be able to make the whole outside block into an async and use await for your axios calls.您应该能够将整个外部块变为async并使用await进行axios调用。

Try something like this:尝试这样的事情:

async mounted() {

   var i = 0;
   const axios = require('axios');
    //Usage

   const response = await axios.get("https://3.11.40.69:9001/app/usageUnitForCustomer/1521/2019-12-30/2020-12-30");

        const time = response.data.event_time;
        const unit = response.data.unit;
        for (i = 0; i < 1; i++) {
          if (time[i] !== time[i + 1]) {
            this.graphData[1].data.time[i] = unit[i];
          }
        } 


...

... continue to use await for the rest of your axios calls. ...继续使用await来处理其余的axios调用。

One issue I see is that you access time[i] in this.graphData[1].data.time[i] = unit[i];我看到的一个问题是你在this.graphData[1].data.time[i] = unit[i];访问time[i] this.graphData[1].data.time[i] = unit[i]; but it is not defined inside your objects.但它没有在您的对象中定义。

Maybe initializing it as an array helps?也许将它初始化为数组有帮助?

data() {
    return {
        graphData: [
          {name: 'Revenue', data: { time: [] }},
          {name: 'Usage', data: { time: [] }}
        ]
    }
}

Edit: Ok, after you edited the expected outcome I see another issue.编辑:好的,在您编辑预期结果后,我看到了另一个问题。

Can you try setting the value like this?您可以尝试设置这样的值吗?

this.graphData[1].data[time[i]] = unit[i];

Note the extra [] around time[i] .注意time[i]周围的额外[] time[i]

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

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