Use a new property in Array

We already saw many times array has build in method or property. Like array.sort(), array.slice().

But we want to use array each value double.

Also we can do it with map

arr = [1,2,3,4];
arr.map(function(item){
   return item * 2;
})


But there we can see we need to write code also for map.

But there I want only array.double()

After that each value should be double.

Array.prototype.double = function(){
  var i;
  for (i = 0; i < this.length; i++) {
    this[i] = this[i] * 2;
  }
}

arr = [1,2,3,4];

arr.double()

arr

[vc_single_image image=”978″ img_size=”full”]

Leave a Reply

Your email address will not be published. Required fields are marked *