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.