使用 NodeJS 过滤并计算 JSON 响应

Filter and count JSON Response using NodeJS

我想过滤并计算信息字段中“赢”和“输”的次数。在这种情况下,赢:1 输:3。如何过滤和计算 response.data 并输出所需的结果。我试过使用带有 include 的过滤器,但没有成功

axios获取方法、过滤和计数:

axios({
    method: "get",
    url: myUrl,
    headers: {
      "Content-type": "application/json"
    },
  })
    .then(function (response) {
        const result = response.data
                res.json(result.filter(data => {
                    if (data.data.info.includes("win")) {
                        return {
                            id: data.data.id,
                            info: data.data.info
                        }
                    }
                    return false
                 }));
      res.json(result);
    })
    .catch(function (error) {
      console.log(error);
    });

这是我从response.data

得到的回复
{
    "data": [{
            "id": "1",
            "name": "Riza",
            "age": "34",
            "info": "dsfgfds dsfg dfsg dsfg fdsg sfg sadf sdf fsadf dfasdf asdf!"
        },
        {
            "id": "2",
            "name": "John",
            "age": "24",
            "info": "dsfgfds sdfg dfsg dsfg fdsg loss sadf sdf sadf dfasdf g!"
        },
        {
            "id": "3",
            "name": "Bill",
            "age": "ff",
            "info": "dsfgfds dsfg dfsg dsfg fdsg win sadf dfgh sadf dfasdf asdf!"
        },
        {
            "id": "4",
            "name": "Sara",
            "age": "55",
            "info": "dsfgfds sdfg dfsg dsfg fdsg loss sadf sdf sadf dfasdf g!"
        },
        {
            "id": "5",
            "name": "Elon",
            "age": "38",
            "info": "dsfgfds sdfg dfsg dsfg fdsg loss sadf sdf sadf dfasdf g!"
        }
    ]
}

我想映射它并用这样的 JSON 结果响应:

{
    "winCount": 1,
    "lossCount": 3,
    "winList": [{
        "id": "3",
        "info": "dsfgfds dsfg dfsg dsfg fdsg win sadf dfgh sadf dfasdf asdf!"
    }],
    "lostList": [{
            "id": "2",
            "info": "dsfgfd..."
        },
        {
            "id": "4",
            "info": "dsfgfds..."
        },
        {
            "id": "5",
            "info": "dsfgfds..."
        }
    ]
}

当然不需要执行此工作,但我喜欢 Lodash 的 partition 功能。此函数将根据谓词将输入数组分割成另外两个数组。第一个数组包含通过谓词的项,第二个数组包含未通过谓词的项。有了这个,我们执行了几个操作...

  1. 将获胜者与其他一切分开。
  2. 将输家与非赢家区分开来。

我们可以这样完成第 1 步:

const [winList, othersWin] = _.partition(data, (datum) => datum.info.includes('win'));

然后像这样完成第 2 步:

const [loseList, othersWinLose] = _.partition(othersWin, (datum) => datum.info.includes('loss'));

然后您可以构建一个报告赢家和输家列表的对象,您的计数就是这些列表的长度...

return {
    winCount: winList.length,
    loseCount: loseList.length,
    winList, loseList
};

最后一行 (winList, loseList) 利用了 JavaScript 的 object initialization shorthand.

这里有一个 StackBlitz 你可以试试看。

axios({
    method: "get",
    url: myUrl,
    headers: {
      "Content-type": "application/json"
    },
  })
    .then(function (response) {
        const result = response.data;

    const filtered = {
        winCount: 0,
        lossCount: 0,
        winList: [],
        lostList: []
    };

    result.map((el, i) => {
        if (el.info.includes('loss')) {
            filtered.lossCount++;
            filtered.lostList.push({
                id: el.id,
                info: el.info
            });
        }
        if (el.info.includes('win')) {
            filtered.winCount++;
            filtered.winList.push({
                id: el.id,
                info: el.info
            });
        }
    });

    console.log(filtered);

      res.json(filtered);
    })
    .catch(function (error) {
      console.log(error);
    });