I have an array which looks like this:
["A", "A", "A", "A", "B", "B", "B", "C", "C", "D", "A", "A", "B", "B", "B"]
How can I filter it, so there is only one unique value at a time, like this:
['A', 'B', 'C', 'D', 'A', 'B']
What I have tried:
I have tried two different approaches, I decided to use a for loop first
function uniqueInOrder(arg) {
var arr = [];
arg.split('');
for(var i = 0; i < arg.length; i++){
if(arg[i] === arg[i+1]) {arr.push(arg[i])}
}
return arr
}
But as far as I understand, I would need to disable it somehow from pushing if it already found the duplicate, because I get this output:
["A", "A", "A", "B", "B", "C", "A", "B", "B"]
Also, I tried this method:
array.filter(function (value, index, self) {
return self.indexOf(value) === index;
})
But that just doesn`t allow duplicates