比较对象数组并删除重复项 [英] Comparing arrays of objects and remove duplicate

查看:67
本文介绍了比较对象数组并删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含对象数组的数组,需要进行比较.

I have an array containing arrays of objects which I need to compare.

我已经浏览了多个相似的线程,但是找不到合适的对象来比较多个对象数组(大多数是比较两个对象数组,或者只是比较对象在单个数组中)

I've looked through multiple similar threads, but I couldn't find a proper one that compares multiple arrays of objects (most are comparing two arrays of objects or just comparing the objects within a single array)

这是数据(下面是带有代码示例的JSFiddle)

This is the data (below is a JSFiddle with code sample)

const data = [  
  [  
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    },
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    }
  ],
  [  
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    },
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    }
  ],
  [  
    {  
      "id": "14",
      "name": "Third one",
      "value": 10
    }
  ]
]

我想删除所有重复的对象数组,而不考虑 data 的长度(可能会有更多记录).

I want to remove all duplicate arrays of objects, regardless of the length of data (there could be a lot more records).

我设法将独特的东西提取到一个对象中

I managed to get the unique ones extracted into an object:

const unique = data.reduce(function(result, obj) {
    return Object.assign(result, obj)
}, [])

这对我来说不起作用,因为我需要保留重复数组中的1个,返回的数据也必须是数组,而不是对象.例如:

That doesn't work for me though, because I need 1 of the duplicated arrays to remain and the returned data to be an array as well, instead of an object. E.g.:

// result I need
[  
  [  
    {  
      "id":"65",
      "name":"Some object name",
      "value":90
    },
    {  
      "id":"89",
      "name":"Second Item",
      "value":20
    }
  ],
  [  
    {  
      "id":"14",
      "name":"Third one",
      "value":10
    }
  ]
]

那么如何将每个对象数组与父对象数组中的其他对象进行比较,并保留每个重复或唯一对象数组中的一个?

So how do I compare each array of objects to the others in the parent array and preserve one of each duplicated or unique array of objects?

JSFiddle

推荐答案

您可以使用function来实现.如下.不确定最佳的最佳做法.

you can achieve so by using function.As below. Not sure about best optimum way of doing so.

var testArray = [  
  [  
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    },
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    }
  ],
  [  
    {  
      "id": "89",
      "name": "Second Item",
      "value": 20
    },
    {  
      "id": "65",
      "name": "Some object name",
      "value": 90
    }
  ],
  [  
    {  
      "id": "14",
      "name": "Third one",
      "value": 10
    }
  ]
]

function removeDuplicatesFromArray(arr){

 var obj={};
 var uniqueArr=[];
 for(var i=0;i<arr.length;i++){ 
    if(!obj.hasOwnProperty(arr[i])){
        obj[arr[i]] = arr[i];
        uniqueArr.push(arr[i]);
    }
 }

return uniqueArr;

}
var newArr = removeDuplicatesFromArray(testArray);
console.log(newArr);

这篇关于比较对象数组并删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆