【问题标题】:How to drop array in nested array如何在嵌套数组中删除数组
【发布时间】:2021-11-30 21:12:50
【问题描述】:

我收到如下 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
            }
        ]
    }

然后,我需要在 status "Inactive"

时删除数组值

我的问题是,当状态 "Inactive" 时,getChartData() 删除数组值的最佳方法是什么?

这是我的代码:

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
  }
}

谁能帮我从嵌套数组中删除值?

谢谢和最好的问候,德德

【问题讨论】:

  • Here my vue code...实际上它只是标准的 Javascript。这些都与 vue 无关。只是为了您的澄清而提及这一点。
  • 哦,抱歉,它是标准的 javascript。以及我如何在 javascript 中做到这一点?

标签: javascript arrays vue.js


【解决方案1】:

您可以使用Array#filter 方法。

例如,

const filtered = dataResponse[e].filter(item => item.status !== "Inactive");

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    相关资源
    最近更新 更多