【问题标题】:Filter and count JSON Response using NodeJS使用 NodeJS 过滤和计数 JSON 响应
【发布时间】:2022-01-14 17:21:28
【问题描述】:

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

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..."
        }
    ]
}

【问题讨论】:

    标签: javascript node.js filter count axios


    【解决方案1】:
    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);
        });
    

    【讨论】:

      【解决方案2】:

      当然不需要执行这项工作,但我喜欢 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,您可以试试看。

      【讨论】:

        猜你喜欢
        • 2019-05-25
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        • 1970-01-01
        • 2016-11-28
        • 2021-10-28
        • 2020-08-09
        • 1970-01-01
        相关资源
        最近更新 更多