You need to enable JavaScript to run this app. 如何删除嵌套数组中status为Inactive的元素并调整getChartData方法
优惠活动
大模型
产品
解决方案
定价
更多

如何删除嵌套数组中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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 23:27:03