【问题标题】:How to loop through a collection and return the items correspond to query params如何遍历集合并返回与查询参数对应的项目
【发布时间】:2021-08-22 02:36:55
【问题描述】:

我正在使用 nodeJS 进行一些查询以从 JSON 文件中检索自定义数据,我想返回该集合中玩得最多的游戏(玩得最多的游戏是用户之间总游戏时间最长的游戏)

这是 JSON:

{
    "data": [
        {
            "userId": 8,
            "game": "League of legends",
            "playTime": 500,
            "genre": "MOBA",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 7,
            "game": "World of warcraft",
            "playTime": 1500,
            "genre": "MMORPG",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 88,
            "game": "Dark Souls",
            "playTime": 109,
            "genre": "Action RPG",
            "platforms": [
                "PS3",
                "Xbox 360",
                "PC",
                "PS4",
                "Xbox One",
                "Nintendo Switch"
            ]
        },
        {
            "userId": 88,
            "game": "The Witcher 3: Wild Hunt",
            "playTime": 9,
            "genre": "RPG",
            "platforms": [
                "PC",
                "PS4",
                "Xbox One",
                "Nintendo Switch"
            ]
        },
        {
            "userId": 1,
            "game": "The last of us 2",
            "playTime": 100,
            "genre": "FPS",
            "platforms": [
                "PS4",
                "PC"
            ]
        },
        {
            "userId": 7,
            "game": "Hitman 3",
            "playTime": 60,
            "genre": "Stealth",
            "platforms": [
                "PS4",
                "PS5",
                "Xbox One",
                "Nintendo Switch",
                "PC"
            ]
        },
        {
            "userId": 99,
            "game": "Minecraft",
            "playTime": 1002,
            "genre": "Sandbox",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 7,
            "game": "Hearthstone",
            "playTime": 1000,
            "genre": "Card Game",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 7,
            "game": "FIFA",
            "playTime": 2000,
            "genre": "Sport",
            "platforms": [
                "PC",
                "PS4",
                "Xbox One"
            ]
        },
        {
            "userId": 2,
            "game": "The Witcher 3: Wild Hunt",
            "playTime": 78,
            "genre": "RPG",
            "platforms": [
                "PC",
                "PS4",
                "Xbox One",
                "Nintendo Switch"
            ]
        },
        {
            "userId": 47,
            "game": "League of legends",
            "playTime": 850,
            "genre": "MOBA",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 2,
            "game": "Among Us",
            "playTime": 5000,
            "genre": "Multiplayer",
            "platforms": [
                "PC",
                "Android"
            ]
        },
        {
            "userId": 2,
            "game": "Valorant",
            "playTime": 2000,
            "genre": "FPS",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 9,
            "game": "Valorant",
            "playTime": 80,
            "genre": "FPS",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 9,
            "game": "Dark Souls",
            "playTime": 109,
            "genre": "RPG",
            "platforms": [
                "PS3",
                "Xbox 360",
                "PC",
                "PS4",
                "Xbox One",
                "Nintendo Switch"
            ]
        },
        {
            "userId": 9,
            "game": "The Witcher 3: Wild Hunt",
            "playTime": 900,
            "genre": "RPG",
            "platforms": [
                "PC",
                "PS4",
                "Xbox One",
                "Nintendo Switch"
            ]
        },
        {
            "userId": 24,
            "game": "League of legends",
            "playTime": 300,
            "genre": "MOBA",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 24,
            "game": "World of warcraft",
            "playTime": 800,
            "genre": "MMORPG",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 54,
            "game": "Minecraft",
            "playTime": 231,
            "genre": "Sandbox",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 7,
            "game": "Minecraft",
            "playTime": 777,
            "genre": "Sandbox",
            "platforms": [
                "PC"
            ]
        },
        {
            "userId": 7,
            "game": "Hitman 3",
            "playTime": 90,
            "genre": "Stealth",
            "platforms": [
                "PS4",
                "PS5",
                "Xbox One",
                "Nintendo Switch",
                "PC"
            ]
        }
    ]
}

我已经写完这个函数了:

const selectTopByPlaytime = async (options) => {
    return _.chain(games)
        .groupBy('game')
        .map((value, key) => {
            return {
                'game': key,
                'playTime': _.reduce(value, function (memo, i) { return memo + i.playTime; }, 0),
                'genre': value[0].genre,
                'platforms': value[0].platforms,
            };
        })
        .sortBy((i) => { return -i.playTime; })
        .first(5).value()

}

我正在尝试通过添加 queryparams 过滤器来修改此函数,以返回发送参数可用的按播放时间排名前列的游戏。

示例:BASE_URL/select_top_by_playtime?genre=FPS 将返回按播放时间分类在 FPS 类型下的顶级游戏。

请帮忙!

【问题讨论】:

    标签: node.js json search filter parameters


    【解决方案1】:

    首先获取查询参数,然后根据它们进行过滤并根据它们的类型进行过滤。

    const selectTopByPlaytime = async (options) => {
      let queryParams = new URLSearchParams(window.location.search);
      return _.chain(games)
        .groupBy("game")
        .filter((item) => {
          for ([key, value] of queryParams.entries()) {
            switch (key) {
              case "genre":
                if (item.genre != value) return false;
              case "platform":
                if (item.platforms.indexOf(value) == -1) return false;
            }
          }
          return true;
        })
        .map((value, key) => {
          return {
            game: key,
            playTime: _.reduce(
              value,
              function (memo, i) {
                return memo + i.playTime;
              },
              0
            ),
            genre: value[0].genre,
            platforms: value[0].platforms,
          };
        })
        .sortBy((i) => {
          return -i.playTime;
        })
        .first(5)
        .value();
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多