I have a response from an API call like below
{
"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
}
]
}
Then, I need to remove array value when status "Inactive"
My question is, what is the best way to do getChartData() to remove array value when status "Inactive"?.
Here my code :
getChartData(dataResponse) {
if (dataResponse) {
const dataChart = [];
Object.keys(dataResponse).forEach((e, index) => {
if (index === 0) {
const b = dataResponse[e].map(r => r.status)
b.unshift("Month")
dataChart.push(b)
}
const a1 = [e]
dataResponse[e].forEach(c => {
a1.push(c.farmer_count)
c.status
})
dataChart.push(a1)
})
this.chartData = dataChart
}
}
Can anyone help me on how to drop values from nested arrays?
Thanks and best regards, Dede
CodePudding user response:
You can use the Array#filter
method.
For example,
const filtered = dataResponse[e].filter(item => item.status !== "Inactive");