如何删除嵌套数组中status为Inactive的元素并调整getChartData方法
解决方法
需要做两处核心调整即可实现需求:
- 遍历每个月份的数组前,先通过
filter方法过滤掉status为Inactive的数组项 - 修正原代码的字段名错误:API返回的玩家计数字段为
player_count,原代码误写为farmer_count,需同步修正
修改后完整代码
getChartData(dataResponse) { if (dataResponse) { const dataChart = []; Object.keys(dataResponse).forEach((e, index) => { // 先过滤掉当前月份数组中status为Inactive的项 const filteredList = dataResponse[e].filter(item => item.status !== 'Inactive') if (index === 0) { const b = filteredList.map(r => r.status) b.unshift("Month") dataChart.push(b) } const a1 = [e] filteredList.forEach(c => { // 修正字段名错误,替换为API返回的正确字段 a1.push(c.player_count) }) dataChart.push(a1) }) this.chartData = dataChart } }
逻辑说明
每次拿到指定月份的数组后,先调用filter方法做筛选,只保留status不等于Inactive的项,后续的表头生成、数值提取都基于筛选后的数组处理,自然就排除了不需要的Inactive相关数据。
内容的提问来源于stack exchange,提问作者dede.brahma
相关产品推荐
相关产品推荐

