Loading article...
The distinctUntilChanged() method filters out consecutive duplicate values from an array, similar to the RxJS operator. It only removes duplicates that appear next to each other, preserving non-consecutive duplicates.
if (!Array.prototype.distinctUntilChanged) { Array.prototype.distinctUntilChanged = function () { const result = []; for (let i = 0; i < this.length; i++) { if (i === 0 || this[i] !== this[i - 1]) { result.push(this[i]); } } return result; }; }
const arr = [1, 1, 2, 2, 2, 3, 1, 1]; const filtered = arr.distinctUntilChanged(); console.log(filtered); // [1, 2, 3, 1]
Test your understanding with 3 quick questions