最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing duplicates from Multidimensional array of objects - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited Jul 12, 2021 at 12:36 E_net4 30.1k13 gold badges114 silver badges151 bronze badges asked Aug 16, 2018 at 2:58 abhididdigiabhididdigi 3714 silver badges16 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

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)

let intervals = [
    [1, 20],
    [1, 20],
    [1, 20]
]
intervals = [...new Set(intervals.map(e => JSON.stringify(e)))].map(e => JSON.parse(e)) // [[1,20]]
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); } );
发布评论

评论列表(0)

  1. 暂无评论