Vuejs如何分配数组对象



我从API调用得到了如下响应

{
"1-2021": [
{
"id": 1,
"status": "New",
"player_count": 7
},
{
"id": 2,
"status": "Verified",
"player_count": 4
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 18
},
{
"id": 4,
"status": "On Playing",
"player_count": 15
},
{
"id": 5,
"status": "Finished",
"player_count": 9
},
{
"id": 6,
"status": "Active",
"player_count": 10
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
],
"2-2021": [
{
"id": 1,
"status": "New",
"player_count": 3
},
{
"id": 2,
"status": "Verified",
"player_count": 8
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 17
},
{
"id": 4,
"status": "On Playing",
"player_count": 11
},
{
"id": 5,
"status": "Finished",
"player_count": 7
},
{
"id": 6,
"status": "Active",
"player_count": 6
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
]
}

然后,我必须在数组中重复整个数组。如何在VueJS中做到这一点?

我搜索了使用forEach..的情况,但没有找到每个用法。

有人能帮助我如何使用forEach或任何其他(VueJS)从数组中获取值吗?

我预料到了结果:

chartData: [
['Month', 'New', 'Verified', 'Regitered ID', 'On Playing', 'Finished', 'Active', 'Inactive'],
['January', 7, 4, 18, 15, 9, 10, 0],
['February', 16, 22, 23, 30, 16, 9, 8]
]

谢谢&问候,

试试这个

let a = {
"1-2021": [{
"id": 1,
"status": "New",
"player_count": 7
},
{
"id": 2,
"status": "Verified",
"player_count": 4
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 18
},
{
"id": 4,
"status": "On Playing",
"player_count": 15
},
{
"id": 5,
"status": "Finished",
"player_count": 9
},
{
"id": 6,
"status": "Active",
"player_count": 10
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
],
"2-2021": [{
"id": 1,
"status": "New",
"player_count": 3
},
{
"id": 2,
"status": "Verified",
"player_count": 8
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 17
},
{
"id": 4,
"status": "On Playing",
"player_count": 11
},
{
"id": 5,
"status": "Finished",
"player_count": 7
},
{
"id": 6,
"status": "Active",
"player_count": 6
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
]
};
let ar = [];
let b = Object.keys(a).forEach((e, index) => {

if (index == 0) {
let b = a[e].map(r => r.status)
b.unshift("Month")
ar.push(b)
}
let a1 = [e]
a[e].forEach(c => {

a1.push(c.player_count)
c.status
})
ar.push(a1)
})
console.log(ar)

最新更新