簡體   English   中英

使用 NodeJS 過濾和計數 JSON 響應

[英]Filter and count JSON Response using NodeJS

我想過濾並計算“贏”和“輸”在信息字段中的次數。 在這種情況下,贏:1,輸:3。如何過濾和計算 response.data 和 output 所需的結果。 我嘗試使用帶有包含的過濾器但沒有運氣

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

我想 map 它並以 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..."
        }
    ]
}
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);
    });

當然不需要執行這項工作,但我喜歡 Lodash 的partition function。 這個 function 將基於謂詞將輸入數組分割成另外兩個 arrays。 第一個數組包含通過謂詞的項目,第二個數組包含失敗的項目。 有了這個,我們執行了幾個操作......

  1. 將贏家與其他一切分開。
  2. 將輸家與非贏家分開。

我們可以像這樣完成第 1 步:

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

然后像這樣完成第 2 步:

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

然后,您可以構建一個 object 報告贏家和輸家列表,您的計數只是這些列表的長度......

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

最后一行( winList, loseList )利用了 JavaScript 的object 初始化簡寫

這里有一個StackBlitz ,你可以試試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM