For example:
const items = ['a', 'a', 'b', 'a', 'a'];
Would become:
['a', 'b', 'a']
Is there a clean way of doing the above using JS?
I have tried:
const items = ['a', 'a', 'b', 'a', 'a'];
const uniqueInRow = [];
for (var i=0; i < items.length; i++) {
item = items[i];
if (item !== item[i++]) {
uniqueInRow.push(item);
}
}
console.log(uniqueInRow);
Try like following:
const items = ['a', 'a', 'b', 'a', 'a'];
const res = []
for(let i = 0; i < items.length; i++) {
if (i === 0 || items[i] !== items[i-1]) res.push(items[i])
}
console.log(res)
You may avoid equal neighbours via oneliner:
vals.forEach((v, i) => (i < vals.length && v === vals[i + 1]) && vals.splice(i, 1))