【发布时间】:2020-04-29 12:30:42
【问题描述】:
我从 API 接收 JSON 格式的数组。我想将它们合并到由对象graphData 组成的嵌套数组中。如何通过一项扩展数据对象?
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: {}}
]
}
}
执行上述代码后,两个数据对象仍然是空的。 结果如下所示,并出现以下错误消息:
0:
name: "Revenue"
data: Object []
1:
name: "Usage"
data: Object []
Uncaught (in promise) TypeError: Cannot set property '0' of undefined
预期结果:
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}}
]
有人知道吗?
【问题讨论】:
-
能否举例说明一下api返回的数据结构以及你要制作的新数据结构?
-
@programmerRaj 我在帖子中编辑了它。
-
好的,我看到输出需要是什么样的,但是输入是什么形式的呢?
-
@programmerRaj 在帖子中编辑。
标签: javascript arrays object vue.js