I have an array ["0", "1", "2"] and I need to create function that makes it
["0", "1","2", "0", "1", "2"]. I wrote that clone function:
arr = [0, 1, 2];
arr.clone = function() {
var b = [];
for (var i = 0; i < arr.length; i++) {
b.push(arr[i]);
}
var c = b.concat(b);
return c;
}
arr.clone();
Am I doing it right? Maybe there's a better or shorter way to clone the elements?
You only have to use concat() by itself, since it builds a new array:
var arr = [0, 1, 2];
arr = arr.concat(arr); // [0, 1, 2, 0, 1, 2]
const arr = [0, 1, 2]
const combinedOne = [ ...arr, ...arr]
With es6, we can use the spread operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With