如何复制js数组中的元素? [英] How to duplicate elements in a js array?
本文介绍了如何复制js数组中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(使用本机" javascript"最简单的方法来复制javascript数组中的每个元素是什么?
Whats the easiest way (with "native" javascript) to duplicate every element in a javascript array?
事项的顺序.
例如:
a = [2, 3, 1, 4]
// do something with a
a
// a is now [2, 2, 3, 3, 1, 1, 4, 4]
推荐答案
我想出了类似 tymeJV 的东西.的答案
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]);
}, []);
这篇关于如何复制js数组中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文