简体   繁体   中英

Groupby array of unique values with lodash

I have an array of ids, I want to have an array of arrays each containing their unique values, for example if I have an array like this:

const arrayOfIds = [1,2,2,4,2,4,5,5];

I want to get a resulting array like the following:

[
    [1],
    [2,2,2],
    [4,4],
    [5,5]
]

Thanks in advance

You can do something like this

 const arrayOfIds = [ 1,2,2,4,2,4,5,5 ]; const output = Object.values(_.groupBy(arrayOfIds)); console.log(output);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

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