javascript 如何复制js数组中的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33305152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:20:55  来源:igfitidea点击:

How to duplicate elements in a js array?

javascriptarrays

提问by Jan Rüegg

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

复制 javascript 数组中每个元素的最简单方法是什么(使用“本机”javascript)?

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]

回答by axelduch

I came up with something similar to tymeJV's answer

我想出了类似于tymeJV的答案

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

回答by morels

Basically:

基本上:

a = [2, 3, 1, 4];
b=[];

for(var i = 0; i< a.length;++i){
  b.push(a[i]);
  b.push(a[i]);
}

a=b;

回答by tymeJV

I suppose you could do:

我想你可以这样做:

var duplicated = a.map(function(item) {
    return [item, item];
}).reduce(function(a, b) { return a.concat(b) });

//duplicated: [2, 2, 3, 3, 1, 1, 4, 4]

回答by Bamieh

I came across this issue in my application, so I created this function:

我在我的应用程序中遇到了这个问题,所以我创建了这个函数:

function duplicateElements(array, times) {
  return array.reduce((res, current) => {
      return res.concat(Array(times).fill(current));
  }, []);
}

To use it, simple pass the array, and the number of times you want the elements to be duplicated:

要使用它,只需传递数组,以及您希望元素被复制的次数:

duplicateElements([2, 3, 1, 4], 2);
// returns: [2, 2, 3, 3, 1, 1, 4, 4]

回答by Zeyad Etman

Basically you can use flatMapin ES19

基本上你可以flatMap在 ES19 中使用

a = [1, 2, 3, 4];
a.flatMap(i => [i,i]); // [1, 1, 2, 2, 3, 3, 4, 4]

Also you can customize the repetitions number like this:

您也可以像这样自定义重复次数:

a = [1, 2, 3, 4];
const dublicateItems = (arr, numberOfRepetitions) => 
    arr.flatMap(i => Array.from({ length: numberOfRepetitions }).fill(i));

dublicateItems(a, 3);

回答by fix-me

how 'bout that?

怎么样?

for (i=a.length-1;i>=0;i--)a.splice(i,0,a[i]);

iterating backwards is undervalued, in this case it keeps the index intact ;)

向后迭代被低估了,在这种情况下它保持索引完整;)

回答by Nina Scholz

Just splice a little bit.

只需拼接一点。

var a = [2, 3, 1, 4],
    i = a.length;
while (i--) {
    a.splice(i, 0, a[i]);
}
document.write('<pre>' + JSON.stringify(a, 0, 4) + '</pre>');

回答by tallgirltaadaa

These functions may help see .sort(), .concat()

这些函数可能有助于查看 .sort(), .concat()

function duplicate(arr) {
    return arr.concat(arr).sort()
} 
console.log(duplicate([1,2,3,4,5]))

回答by yckart

0/2  =  0    =  0  |0  =  0
1/2  =  0.5  =  0.5|0  =  0
2/2  =  1    =  1  |0  =  1
3/2  =  1.5  =  1.5|0  =  1
4/2  =  2    =  2  |0  =  2
5/2  =  2.5  =  2.5|0  =  2
6/2  =  3    =  3  |0  =  3
7/2  =  3.5  =  3.5|0  =  3

Treat |0as Math.floor

治疗|0Math.floor



In code this could look like this:

在代码中,这可能如下所示:

for (let i = 0; i < a.length * 2; i++) {
  a[i] = a[i / 2 |?0]
}

Because immutability is preferable, one could do something like this:

因为不变性是可取的,所以可以做这样的事情:

function repeatItems(a, n) {
  const b = new Array(a.length * n)
  for (let i = 0; i < b.length; i++) {
    b[i] = a[i / n | 0]
  }
  return b
}

Unreadable ES6 spaghetti code:

不可读的 ES6 意大利面代码:

const repeatItems = (a, n) => Array.from(Array(a.length * n), (_, i) => a[i / n | 0])

回答by radzak

ES6 way of life (based on axelduch'sanswer)

ES6 的生活方式(基于axelduch 的回答)

const arr = [2, 3, 1, 4].reduce((res, current) => [...res, current, current], []);

console.log(arr);