How to duplicate elements in a js array?

后端 未结 11 545
慢半拍i
慢半拍i 2020-12-03 20:46

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

The order matters.

For example:



        
11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 21:18

    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]);
    }, []);
    

提交回复
热议问题