-1

This is what I need to make work in JavaScript: 

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

Best practices? Ideas?

Tushar
  • 85,780
  • 21
  • 159
  • 179
j_d
  • 2,818
  • 9
  • 50
  • 91
  • 1
    What are you asking for? How to write the content of `duplicate` or how to make it so that you can custom define a method `duplicate` on an array object? – Frank V Jun 24 '15 at 13:28
  • @FrankV The second of those – j_d Jun 24 '15 at 13:34

1 Answers1

3

Use concat on the same array. It'll duplicate the elements of the array.

The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.

// Define method on prototype so that it can be directly called on array
Array.prototype.duplicate = function() {
    return this.concat(this);
};
Tushar
  • 85,780
  • 21
  • 159
  • 179