Javascript Array duplicate()

Description

Javascript Array duplicate()



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



console.log([1,2,3,4].duplicate())// w  w  w .  j  a va 2 s  .  c o  m

Javascript Array duplicate()

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

Array.prototype.duplicate = function() {
    return (this + ',' + this);
};

Javascript Array duplicate()

module.exports = Array;// ww w  .j a  v  a  2  s.co  m
Array.prototype.duplicate = function(){
  var arrayVal = this;
  this.forEach(function(val){
    arrayVal.push(val);
  })
  return arrayVal;
}

Javascript Array duplicate()

// 10/09/15//from   w w w .j a  va  2s.co  m
//prototype practice

Array.prototype.duplicate = function(){
  var newArr = this.slice();
  for(var i = 0; i < this.length; i ++){
    newArr.push(this[i]);
  }
  return newArr;
};

console.log([1, 2, 3, 4].duplicate());

Javascript Array duplicate()

function duplicateArray(arr) {

  return arr.concat(arr);

}

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

console.log([1,2,3,4,5].duplicate());//from w w  w.j  a v a 2s .co m

console.log(duplicateArray([1,2,3,4,5]));

//i: [1,2,3,4,5]
//o: [1,2,3,4,5,1,2,3,4,5]
//tc: O(n)
//sc: O(n)

Javascript Array duplicate()

// Without using any libraries, write a function in JavaScript which does the following:
///*from  w  w w  . j  a v a  2 s . co m*/
// duplicate([1,2,3,4,5]); // [1,2,3,4,5,1,2,3,4,5]


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

duplicate([1,2,3,4,5])

Javascript Array duplicate()

var arr1 = [3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];


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

console.log([4, 5, 6, 7, 7].duplicate());

Javascript Array duplicate()

//[1,2,3].duplicate() //[1,2,3,1,2,3]

Array.prototype.duplicate = function () {
    var length = this.length;
    for (var i = 0; i < length; i++) {
        this.push(this[i]);//from www. j a v a2s  .  c  o m
    }
    return this;
};

console.log('Duplicated array: ' + JSON.stringify([1, 2, 3].duplicate()));

Javascript Array duplicate()

//implement duplicate([1, 2, 3, 4, 5]) // [1,2,3,4,5,1,2,3,4,5]


function duplicate(arr) {
    var len = arr.length;
    for (var i = 0; i < len; i++) {
        console.log(arr[i]);//from ww w  . ja  v a 2  s  .co  m
        arr[len + i] = arr[i];
    }
    return arr;
}

// or [1,2,3].duplicate();

Array.prototype.duplicate = function () {
    var len = this.length;
    for (var i = 0; i < len; i++) {
        this[len + i] = this[i];
    }
    return this;
}

Javascript Array duplicate()

/**/*from   ww w.jav  a 2s. co  m*/
 * Make this work in JavaScript:

 [1,2,3,4,5].duplicate();

 */


Array.prototype.duplicate = function () {
    var arr = this;
    return this.push.apply(arr,arr);
};

//Array.prototype.duplicate=function(){
//    var arr = this;
//    for(var i = 0; i < arr.length; i++){
//        this.push(arr[i]);
//        //console.log(arr[i]);
//    }
//};

var list = [1,2,3,4,5];
list.duplicate();
console.log(list);

Javascript Array duplicate()

/*// w ww.j  a  v  a 2  s  . c o  m
 Native Method: like map, filter are from the Array which are built-in methods. 

 How to add native methods:

 1. Add a method to a Data type's prototype.
 2. It will be available throughout the application
*/ 

// Example - extending a method in Array
Array.prototype.duplicate = function() {
 return this.slice(0);
};

console.log([1,2,3].duplicate()); // [1,2,3]



PreviousNext

Related