This doesn't work:
result = [1, 4, 5, 5, 5];
var newData = result.reduce(function(acc, cur) {
if (cur in acc === false) {
acc.push(cur);
}
return acc;
},[]);
console.log(newData);
Output:
[1, 4, 5, 5, 5]
I know one workable way, like replace the if()
condition with the following code:
if (acc.length == 0 || acc[acc.length-1] !== cur) {
acc.push(cur);
}
which gives expected output:
[1, 4, 5]
My question is why the first way doesn't work?