如何从对象数组中删除重复项

时间:2020-02-17 20:22:28

标签: javascript reduce

我有一个JSON数据,但是我需要删除重复项。

唯一的约束是如果有重复的消息,我们希望 如果uuidcontent相同,则将它们进行重复数据删除。

目前这是我的尝试,但我遇到了困难:

const result = test['messages'].reduce((accu,cur)=>{
   if(! accu[curr.uuid] && accu[curr.content]){
    return accu.push(cur)
   }
},[])

这是测试示例:

   let test=  {
      "messages": [
        {
          "sentAt": "2012-11-13T17:29:37.003Z",
          "uuid": "435453",
          "content": "1",
          "senderUuid": "2"
        },
        {
          "sentAt": "2015-05-22T13:55:10.542Z",
          "uuid": "4354353",
          "content": "2",
          "senderUuid": "2"
        },
        {
          "sentAt": "2012-11-20T01:31:33.751Z",
          "uuid": "4354353",
          "content": "3",
          "senderUuid": "1"
        },
        {
          "sentAt": "2016-02-17T10:13:03.115Z",
          "uuid": "435453",
          "content": "4",
          "senderUuid": "2"
        },
        {
          "sentAt": "2015-05-22T13:55:10.542Z",
          "uuid": "4354353",
          "content": "2",
          "senderUuid": "1"
        },
        {
          "sentAt": "2018-07-05T10:19:07.713Z",
          "uuid": "4354353",
          "content": "6",
          "senderUuid": "2"
        },
        {
          "sentAt": "2016-11-09T03:24:54.612Z",
          "uuid": "4354353",
          "content": "7",
          "senderUuid": "1"
        },
        {
          "sentAt": "2013-06-21T16:39:08.630Z",
          "uuid": "43543353",
          "content": "8",
          "senderUuid": "1"
        },
        {
          "sentAt": "2013-08-24T01:55:38.167Z",
          "uuid": "43521314353",
          "content": "9",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-11-05T11:37:00.472Z",
          "uuid": "43532134353",
          "content": "10",
          "senderUuid": "2"
        },
        {
          "sentAt": "2017-09-26T17:01:10.949Z",
          "uuid": "43321315433",
          "content": "11",
          "senderUuid": "2"
        },
        {
          "sentAt": "2012-12-02T13:55:30.626Z",
          "uuid": "43543321353",
          "content": "12",
          "senderUuid": "1"
        },
        {
          "sentAt": "2015-05-03T08:54:02.530Z",
          "uuid": "43542233",
          "content": "13",
          "senderUuid": "2"
        },
        {
          "sentAt": "2013-08-24T01:55:38.167Z",
          "uuid": "43521314353",
          "content": "9",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-02-19T09:42:11.913Z",
          "uuid": "435353",
          "content": "15",
          "senderUuid": "2"
        },
        {
          "sentAt": "2018-07-06T20:31:01.649Z",
          "uuid": "4354543353",
          "content": "16",
          "senderUuid": "2"
        },
        {
          "sentAt": "2017-09-25T04:35:18.647Z",
          "uuid": "4354353",
          "content": "17",
          "senderUuid": "1"
        },
        {
          "sentAt": "2012-12-17T15:08:37.988Z",
          "uuid": "43545433353",
          "content": "18",
          "senderUuid": "1"
        },
        {
          "sentAt": "2016-02-03T05:20:52.506Z",
          "uuid": "435454www35353",
          "content": "19",
          "senderUuid": "2"
        },
        {
          "sentAt": "2018-04-07T06:30:38.178Z",
          "uuid": "43545435353",
          "content": "20",
          "senderUuid": "2"
        }
      ]
    }

1 个答案:

答案 0 :(得分:0)

您可以使用Set

const unique = new Set(test['messages'].map(e => JSON.stringify(e)));

关于套装:

Set对象是值的集合。您可以按插入顺序遍历集合的元素。集合中的值只能出现一次;它在Set的集合中是独一无二的。