Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to double array values

I have an array:

arr = ["a", "b", c"]

I want to double values like

arr = ["a", "b", c", "a", "b", "c"]

(not in particular order).

What's the best way to do it in JavaScript?

like image 610
Miqez Avatar asked Apr 23 '17 18:04

Miqez


2 Answers

Possible solution, using Array#concat.

var arr = ["a", "b", "c"],
    res = arr.concat(arr);
    
    console.log(res);
like image 197
kind user Avatar answered Sep 26 '22 13:09

kind user


You could also use ES6 spread syntax with Array.push:

let arr = ['a', 'b', 'c'];
arr.push(...arr);

console.log(arr);
like image 45
Rob M. Avatar answered Sep 23 '22 13:09

Rob M.