简体   繁体   中英

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:

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

How do I do this using loadash/ underscore? 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. 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() :

 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:

 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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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