【发布时间】:2020-03-04 00:34:39
【问题描述】:
在 Vue 中,我收到了来自 axios 的响应,我将其转储到我的控制台中。它在数据方面看起来不错,但是由于注释类型不同,返回的数据对象每个 item_id 有多个行。这导致我无法在前端每行使用一条记录。
我需要找到正确的方法,让每个 item_id 只有一个主级别,并将 cmets 嵌套在该级别下的一个级别中,这样即使一条记录有 5 种不同的评论/文本类型,它也只是一个 id 的一部分响应记录(我可以在前端使用比较来在某些地方显示某些评论/文本类型)。
在 this.events 的 console.log 中,我得到:
0:
item_id: 12,
item_text_type: comment,
item_text: "this has been updated",
date: "2020/03/03"
1:
item_id: 12,
item_text_type: title,
item_text: "new entry",
date: "2020/03/03"
2:
item_id: 13,
item_text_type: comment,
item_text: "this has been updated",
date: "2020/03/03"
3:
item_id: 13,
item_text_type: title,
item_text: "2nd Entry",
date: "2020/03/03"
我想让它们变成更像这样的结构:
0:
item_id: 12,
date: "2020/03/03",
comments:
item_text_type: comment,
item_text: "this has been updated"
item_text_type: title,
item_text: "new entry"
0:
item_id: 13,
date: "2020/03/03"
comments:
item_text_type: comment,
item_text: "this has been updated"
item_text_type: title,
item_text: "2nd entry"
所以基本上,每个 item_id 只有一条记录,并将 cmets 组合成一个子数组或类似的东西。其他信息(例如日期)将在 id 级别。
我怎样才能改变这个来达到这个结果
fetchTasks() {
axios.get('/home')
.then(response => {
//handle success
this.events = response.data.map(({item_comment, ...item}) => ({
...item,
title: item_comment
}));
console.log(this.events);
})
.catch(function(error) {
// handle error
console.log(error)
})
.finally(function() {
})
},
【问题讨论】:
标签: javascript vue.js