I have:
['a', 'b', 'd', 'a', 'f', 'b', 'a', 'b']
I'd like to have:
[['a', 'a', 'a'], ['b', 'b', 'b'], ['d'], ['f']]
I have:
['a', 'b', 'd', 'a', 'f', 'b', 'a', 'b']
I'd like to have:
[['a', 'a', 'a'], ['b', 'b', 'b'], ['d'], ['f']]
You can do it like this:
const test = ["a", "b", "d", "a", "f", "b", "a", "b"]
function parseElements(elements) {
const temp = {}
for (const element of test) {
if (!temp[element]) {
temp[element] = []
}
temp[element].push(element)
}
return [...Object.values(temp)]
}
const result = parseElements(test)