Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

Groupby array of unique values with lodash

Hi everybody, 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:

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

I want to get a resulting array ike the following:

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

Thanks in advance

0 likes
1 reply
tykus's avatar

Assuming the 22 is a typo:

_.toArray(_.groupBy(arrayOfIds, (i) => i))

Please or to participate in this conversation.