Javascript Array decimalfy()

Description

Javascript Array decimalfy()




const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

//for loop/*from  w ww.  j  a  v a2 s. c o  m*/
console.log('Looping using for')
for (let i = 0; i < digits.length; i++) {
  console.log(digits[i]);
}

//for in
console.log('Looping using for in')
for (const index in digits) {
  console.log(digits[index]);
}

/*
    for in iterates over new function added to array prototype
*/
console.log('Add function to array prototype')
Array.prototype.decimalfy = function() {
    for (let i = 0; i < this.length; i++) {
      this[i] = this[i].toFixed(2);
    }
  };
  
  
  for (const index in digits) {
    console.log(digits[index]);
  }

Javascript Array decimalfy()

/*// ww w  .ja va  2 s.  com
Because for...in loops loop over all enumerable properties, this means if you add any 
additional properties to the array's prototype, then those properties will also appear 
in the loop.
*/
Array.prototype.decimalfy = function() {
  for (let i = 0; i < this.length; i++) {
    this[i] = this[i].toFixed(2);
  }
};

const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (const index in digits) {
  console.log(digits[index]);   // will display [Function]
}

for (const digit of digits) {
  console.log(digit);           // Works fine
}



PreviousNext

Related