首页 > 解决方案 > 如何从对象数组中获取具有特定键的唯一值的对象?

问题描述

我有一个对象数组,如下所示。

 [{
        "_id": {
          "$oid": "5d9a16764f66ef0017a738ee"
        },
        "user": "tVH3U5Va4cBFiATvAACD",
        "team": "team1",
        "question": "5d98c7109d0e1d0017e3d31f",
        "date": {
          "$date": "2019-10-06T16:29:40.240Z"
        },
        "correct": "true",
        "ownId": "4",
        "blockId": "1"
    },
    {
        "_id": {
          "$oid": "5d9a167c4f66ef0017a738ef"
        },
        "user": "tVH3U5Va4cBFiATvAACD",
        "team": "team1",
        "question": "5d98c7109d0e1d0017e3d31f",
        "date": {
          "$date": "2019-10-06T16:29:46.694Z"
        },
        "correct": "true",
        "ownId": "4",
        "blockId": "1"
    },
    {
        "_id": {
          "$oid": "5d9a16824f66ef0017a738f0"
        },
        "user": "tVH3U5Va4cBFiATvAACD",
        "team": "team1",
        "question": "5d98c7109d0e1d0017e3d31e",
        "date": {
          "$date": "2019-10-06T16:29:52.900Z"
        },
        "correct": "true",
        "ownId": "5",
        "blockId": "1"
    }]

我需要使用 last 获取date具有唯一userownIdblockId. 唯一我的意思是我只会得到一个user相同ownIdblockId。对于此示例,我只想获取,因为数组中的第一个对象和数组中的最后一个对象具有相同user的 ,ownIdblockId.

[{
            "_id": {
              "$oid": "5d9a167c4f66ef0017a738ef"
            },
            "user": "tVH3U5Va4cBFiATvAACD",
            "team": "team1",
            "question": "5d98c7109d0e1d0017e3d31f",
            "date": {
              "$date": "2019-10-06T16:29:46.694Z"
            },
            "correct": "true",
            "ownId": "4",
            "blockId": "1"
        },
        {
            "_id": {
              "$oid": "5d9a16824f66ef0017a738f0"
            },
            "user": "tVH3U5Va4cBFiATvAACD",
            "team": "team1",
            "question": "5d98c7109d0e1d0017e3d31e",
            "date": {
              "$date": "2019-10-06T16:29:52.900Z"
            },
            "correct": "true",
            "ownId": "5",
            "blockId": "1"
        }]

我尝试的是遍历数组,但是这样我只能在一个键中获得唯一的对象。我不知道如何用几个键来拥有它。

stat.forEach(function(item) {
  var i = unique.findIndex(x => x.user == item.user);
  if (i <= -1) {
    unique.push({
      id: item._id,
      user: item.user
    });
  }
});

标签: javascriptarrays

解决方案


您可以使用reduce()方法迭代数组,以构建哈希表或对象,并将其user + ownId + blockId作为属性名称或哈希键。

迭代时,如果存在具有相同键的对象,则比较日期并将值替换为具有最新日期的对象。

var data = [{ "_id": { "$oid": "5d9a16764f66ef0017a738ee" }, "user": "tVH3U5Va4cBFiATvAACD", "team": "team1", "question": "5d98c7109d0e1d0017e3d31f", "date": { "$date": "2019-10-06T16:29:40.240Z" }, "correct": "true", "ownId": "4", "blockId": "1" }, { "_id": { "$oid": "5d9a167c4f66ef0017a738ef" }, "user": "tVH3U5Va4cBFiATvAACD", "team": "team1", "question": "5d98c7109d0e1d0017e3d31f", "date": { "$date": "2019-10-06T16:29:46.694Z" }, "correct": "true", "ownId": "4", "blockId": "1" }, { "_id": { "$oid": "5d9a16824f66ef0017a738f0" }, "user": "tVH3U5Va4cBFiATvAACD", "team": "team1", "question": "5d98c7109d0e1d0017e3d31e", "date": { "$date": "2019-10-06T16:29:52.900Z" }, "correct": "true", "ownId": "5", "blockId": "1" } ];

var result = Object.values(data.reduce((acc, curr) => {
  let value = acc[curr.user + curr.ownId + curr.blockId];

  if (!value || new Date(curr.date.$date) > new Date(value.date.$date)) {
    acc[curr.user + curr.ownId + curr.blockId] = curr;
  }

  return acc;
}, {}));

console.log(result);


推荐阅读