admin管理员组文章数量:1399935
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 03 Answers
Reset to default 4I 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); } );
本文标签: javascriptRemoving duplicates from Multidimensional array of objectsStack Overflow
版权声明:本文标题:javascript - Removing duplicates from Multidimensional array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744137866a2592481.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论