Problem and Solutions
Unable to monkey patch array using uniq in JS [beginner] in node js.
By M.K. Verma · 2 May 2022
![Unable to monkey patch array using uniq in JS [beginner] in node js.](/_next/image?url=https%3A%2F%2Fserveapi.trimwebsolutions.com%2Fuploads%2Flegacy%2Fblog%2F16514862931651486293.jpg&w=3840&q=65)
- You need to declare
narr - Your condition in your for loop makes no sense
i < 0. You need the length of the arraythis.length - Typo
includeitsincludes
Array.prototype.uniq = function() {
let narr = [];
for (let i = 0; i < this.length; i++) {
if (!narr.includes(this[i])) {
narr.push(this[i]);
}
}
return narr;
}
console.log([1, 2, 2, 3, 3, 3].uniq());