Friday, 7 February 2014

Javascript User Defined Duplicate function in Array

A value which you want duplicate nothing but copy of  an array or value you can use duplicate function which is user defined function in javascript

Ex: 

var a = [1,2,3,4,5];

If i want to get duplicate this array there is no function in javascript so now i am using user defined javascript function in Array.


we have Array in javascript by using this we can move forward


Array.prototype.duplicate = function() {
    return this.concat(this);
};

By running above function in console you can get a function in array just follow the below picture




var c = a.duplicate();
console.log(c);
Ouput is :[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

1 comment: