简体   繁体   English

从对象的多维数组中删除重复项

[英]Removing duplicates from Multidimensional array of objects

The problem is a simple one.问题很简单。 I have an array of array of objects.我有一组对象数组。 Example:例子:

[[{'name':'cat"}, {'name':'lion'}], [{'name':'elephant"},
{'name':'lion'}, {'name':'dog'}], [{'name':'tiger"}, {'name':'mouse'},
{'name':'dog'}]]

I want to remove duplicates using JS and retain only their first occurrences and remove their duplicates resulting in:我想使用 JS 删除重复项并仅保留它们的第一次出现并删除它们的重复项,从而导致:

[[{'name':'cat"}, {'name':'lion'}], [{'name':'elephant"},
{'name':'dog'}], [{'name':'tiger"}, {'name':'mouse'}]]

How do I do this using loadash/ underscore?我如何使用 loadash/下划线来做到这一点? I have tried a bunch of options, first one involved passing an iteratee to the .uniq function, but it didn't work the way I wanted since these are 2d array.我尝试了很多选项,第一个涉及将迭代对象传递给.uniq函数,但它没有按我想要的方式工作,因为这些是二维数组。 The other brute force method included iterating through all the arrays one by one and then doing a search to see if any of the other arrays contain the element and removing the element.另一种蛮力方法包括逐个遍历所有数组,然后进行搜索以查看是否有任何其他数组包含该元素并删除该元素。 Repeat this procedure until the last-1 of the sub-arrays.重复这个过程直到最后一个子数组。

Is don't like the brute force method, so wondering if there are any other methods I can retain unique elements.不喜欢蛮力方法,所以想知道是否还有其他方法可以保留独特的元素。

Note that this is a 2D array of objects and all the posts on stack overflow answer 1D array of objects.请注意,这是一个二维对象数组,堆栈溢出的所有帖子都回答一维对象数组。 I'm not looking to find duplicates in an array.我不想在数组中找到重复项。 I'm trying to find duplicates across all arrays in an array.我正在尝试在数组中的所有数组中查找重复项。

I nice way to deal with this is with a Set , just add your labels to the set as you filter() inside a map() :我处理这个问题的好方法是使用Set ,只需在map() filter()将标签添加到集合中:

 let arr = [ [{'name':'cat'}, {'name':'lion'}], [{'name':'elephant'},{'name':'lion'}, {'name':'dog'}], [{'name':'tiger'}, {'name':'mouse'}, {'name':'dog'}] ] let known = new Set() let filtered = arr.map(subarray => subarray.filter(item => !known.has(item.name) && known.add(item.name)) ) console.log(filtered)

EDIT: If you can't use a Set, you can get the same function from a regular object:编辑:如果您不能使用 Set,则可以从常规对象获得相同的功能:

 let arr = [ [{'name':'cat'}, {'name':'lion'}], [{'name':'elephant'},{'name':'lion'}, {'name':'dog'}], [{'name':'tiger'}, {'name':'mouse'}, {'name':'dog'}] ] let known = {} let filtered = arr.map(subarray => subarray.filter(item => !known.hasOwnProperty(item.name) && (known[item.name] = true)) ) console.log(filtered)

var a = [];
a.push( [1,2,3], [4,5,6], [1,2,3], [4,5,6] );
a.forEach( ( chunk, idx ) => { a[idx] = JSON.stringify(chunk); } );
a = [ ...new Set(a) ];
a.forEach( ( chunk, idx ) => { a[idx] = JSON.parse(chunk); } );
let intervals = [
    [1, 20],
    [1, 20],
    [1, 20]
]
intervals = [...new Set(intervals.map(e => JSON.stringify(e)))].map(e => JSON.parse(e)) // [[1,20]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM