Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

Unable to monkey patch array using uniq in JS [beginner] in node js.

Unable to monkey patch array using uniq in JS [beginner] in node js.
  1. You need to declare narr
  2. Your condition in your for loop makes no sense i < 0. You need the length of the array this.length
  3. Typo include its includes

 

 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());

7 0

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online