从数组中删除重复的对象,但合并嵌套的对象

时间:2019-07-04 14:54:36

标签: javascript arrays object ecmascript-6

当前有一系列包含游戏发行版的对象。但是,游戏发行可以在多个平台上进行,并且在阵列中显示为单独的对象。我希望通过比较游戏ID来删除重复的游戏,但合并平台对象

我尝试使用reduce函数,该函数可以通过游戏ID成功删除重复的对象,但是我无法使其适应合并平台

const filteredArr = data.reduce((acc, current) => {
  const x = acc.find(item => item.game.id === current.game.id);

  if (!x) {
    return acc.concat([current]);
  } else {
    return acc;
  }
}, []);

当前数组:

const data = [{
  "id": 157283,
  "date": 1553212800,
  "game": {
    "id": 76882,
    "name": "Sekiro: Shadows Die Twice",
    "popularity": 41.39190295640344
  },
  "human": "2019-Mar-22",
  "m": 3,
  "platform": {"id": 48, "name": "PlayStation 4"},
  "region": 8,
  "y": 2019
}, {
  "id": 12,
  "date": 1553212800,
  "game": {
    "id": 76832,
    "name": "Spiderman",
    "popularity": 41.39190295640344
  },
  "human": "2019-Mar-22",
  "m": 3,
  "platform": {"id": 6, "name": "PC (Microsoft Windows)"},
  "region": 8,
  "y": 2019
}, {
  "id": 157283,
  "date": 1553212800,
  "game": {
    "id": 76882,
    "name": "Sekiro: Shadows Die Twice",
    "popularity": 41.39190295640344
  },
  "human": "2019-Mar-22",
  "m": 3,
  "platform": {"id": 48, "name": "Xbox"},
  "region": 8,
  "y": 2019
}]

合并后的预期格式:

[{
  "id": 157283,
  "date": 1553212800,
  "game": {
    "id": 76882,
    "name": "Sekiro: Shadows Die Twice",
    "popularity": 41.39190295640344
  },
  "human": "2019-Mar-22",
  "m": 3,
  "platforms": ["PlayStation", "Xbox"],
  "region": 8,
  "y": 2019
}, {
  "id": 12,
  "date": 1553212800,
  "game": {
    "id": 76832,
    "name": "Spiderman",
    "popularity": 41.39190295640344
  },
  "human": "2019-Mar-22",
  "m": 3,
  "platforms": ["Playstation"],
  "region": 8,
  "y": 2019
}]

4 个答案:

答案 0 :(得分:2)

您真的很亲近,您只需要稍微更改一下逻辑即可。您可以尝试以下操作;一个示例-https://repl.it/@EQuimper/ScaryBumpyCircle

const filteredArr = data.reduce((acc, current) => {
  const x = acc.find(item => item.game.id === current.game.id);

  if (!x) {
    current.platform = [current.platform]
    acc.push(current);
  } else {
    x.platform.push(current.platform);
  }

  return acc;
}, []);

返回值为

[
  {
    "id": 157283,
    "date": 1553212800,
    "game": {
      "id": 76882,
      "name": "Sekiro: Shadows Die Twice",
      "popularity": 41.39190295640344
    },
    "human": "2019-Mar-22",
    "m": 3,
    "platform": [
      {
        "id": 48,
        "name": "PlayStation 4"
      },
      {
        "id": 48,
        "name": "Xbox"
      }
    ],
    "region": 8,
    "y": 2019
  },
  {
    "id": 12,
    "date": 1553212800,
    "game": {
      "id": 76832,
      "name": "Spiderman",
      "popularity": 41.39190295640344
    },
    "human": "2019-Mar-22",
    "m": 3,
    "platform": [
      {
        "id": 6,
        "name": "PC (Microsoft Windows)"
      }
    ],
    "region": 8,
    "y": 2019
  }
]

如果您只想拥有一系列平台字符串,请选择

const filteredArr = data.reduce((acc, current) => {
  const x = acc.find(item => item.game.id === current.game.id);

  if (!x) {
    current.platform = [current.platform.name]
    acc.push(current);
  } else {
    x.platform.push(current.platform.name);
  }

  return acc;
}, []);

现在返回值是

[
  {
    "id": 157283,
    "date": 1553212800,
    "game": {
      "id": 76882,
      "name": "Sekiro: Shadows Die Twice",
      "popularity": 41.39190295640344
    },
    "human": "2019-Mar-22",
    "m": 3,
    "platform": [
      "PlayStation 4",
      "Xbox"
    ],
    "region": 8,
    "y": 2019
  },
  {
    "id": 12,
    "date": 1553212800,
    "game": {
      "id": 76832,
      "name": "Spiderman",
      "popularity": 41.39190295640344
    },
    "human": "2019-Mar-22",
    "m": 3,
    "platform": [
      "PC (Microsoft Windows)"
    ],
    "region": 8,
    "y": 2019
  }
]

答案 1 :(得分:0)

您可以将对象的platform分开,看看是否有一个具有相同的id的对象,然后将数组添加平台,而不创建新的数据集。

const
    data = [{ id: 157283, date: 1553212800, game: { id: 76882, name: "Sekiro: Shadows Die Twice", popularity: 41.39190295640344 }, human: "2019-Mar-22", m: 3, platform: { id: 48, name: "PlayStation 4" }, region: 8, y: 2019 }, { id: 12, date: 1553212800, game: { id: 76832, name: "Spiderman", popularity: 41.39190295640344 }, human: "2019-Mar-22", m: 3, platform: { id: 6, name: "PC (Microsoft Windows)" }, region: 8, y: 2019 }, { id: 157283, date: 1553212800, game: { id: 76882, name: "Sekiro: Shadows Die Twice", popularity: 41.39190295640344 }, human: "2019-Mar-22", m: 3, platform: { id: 48, name: "Xbox" }, region: 8, y: 2019 }],
    result = data.reduce((r, { platform, ...o }) => {
        var temp = r.find(({ id }) => id === o.id);
        if (!temp) r.push(temp = { ...o, platforms: [] });
        temp.platforms.push(platform);
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

请看看:

const data = [    {        "id": 157283,
        "date": 1553212800,
        "game": {
            "id": 76882,
            "name": "Sekiro: Shadows Die Twice",
            "popularity": 41.39190295640344
        },
        "human": "2019-Mar-22",
        "m": 3,
        "platform": {
            "id": 48,
            "name": "PlayStation 4"
        },
        "region": 8,
        "y": 2019
    },
    {
        "id": 12,
        "date": 1553212800,
        "game": {
            "id": 76832,
            "name": "Spiderman",
            "popularity": 41.39190295640344
        },
        "human": "2019-Mar-22",
        "m": 3,
        "platform": {
            "id": 6,
            "name": "PC (Microsoft Windows)"
        },
        "region": 8,
        "y": 2019
    },{        "id": 157283,
    "date": 1553212800,
    "game": {
        "id": 76882,
        "name": "Sekiro: Shadows Die Twice",
        "popularity": 41.39190295640344
    },
    "human": "2019-Mar-22",
    "m": 3,
    "platform": {
        "id": 48,
        "name": "Xbox"
    },
    "region": 8,
    "y": 2019
},
]

const filteredArr = data.reduce((acc, current) => {
  const x = acc.find(item => item.game.id === current.game.id);
    if (!x) {
      current.platform = [current.platform.name]
      return acc.concat([current]);
    } else {
      x.platform.push(current.platform.name);
      return acc;
    }
  }, []);
console.log(filteredArr);

答案 3 :(得分:0)

这是另一种解决方案,使用forEach代替reduce。这利用了一个查找哈希,该哈希使如果处理大量数据的速度更快,则可以使用find

const data = [{"id": 157283, "date": 1553212800, "game": {"id": 76882, "name": "Sekiro: Shadows Die Twice", "popularity": 41.39190295640344}, "human": "2019-Mar-22", "m": 3, "platform": {"id": 48, "name": "PlayStation 4"}, "region": 8, "y": 2019}, {"id": 12, "date": 1553212800, "game": {"id": 76832, "name": "Spiderman", "popularity": 41.39190295640344}, "human": "2019-Mar-22", "m": 3, "platform": {"id": 6, "name": "PC (Microsoft Windows)"}, "region": 8, "y": 2019}, {"id": 157283, "date": 1553212800, "game": {"id": 76882, "name": "Sekiro: Shadows Die Twice", "popularity": 41.39190295640344}, "human": "2019-Mar-22", "m": 3, "platform": {"id": 48, "name": "Xbox"}, "region": 8, "y": 2019}];

let result = {};

data.forEach(({platform, ...release}) => {
  release.platforms = [platform.name];

  const releaseLookup = result[release.game.id];
  if (!releaseLookup) {
    result[release.game.id] = release;
  } else {
    releaseLookup.platforms.push(...release.platforms);
  }
});

console.log(Object.values(result));