Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  94] [ 7]  / answers: 1 / hits: 55968  / 9 Years ago, fri, october 23, 2015, 12:00:00

Whats the easiest way (with native javascript) to duplicate every element in a javascript array?



The order matters.



For example:



a = [2, 3, 1, 4]
// do something with a
a
// a is now [2, 2, 3, 3, 1, 1, 4, 4]

More From » arrays

 Answers
29

I came up with something similar to tymeJV's answer



[2, 3, 1, 4].reduce(function (res, current, index, array) {
return res.concat([current, current]);
}, []);

[#64624] Wednesday, October 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karivictoriab

Total Points: 530
Total Questions: 90
Total Answers: 95

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;