且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

串联阵列本身以复制它

更新时间:2023-08-19 19:06:58

您不能繁殖数组在JavaScript中。使用当前解决方案的 Array.concat 是正确的。
注意,它创建具有复制的项目的单独阵列,并且不改变原始。

In Python:

>>> [1,2,3,4] * 2 
[1, 2, 3, 4, 1, 2, 3, 4]

What is the way to go with JavaScript ?

Currently using:

var data = [1,2,3,4];
data.concat(data);
//[1, 2, 3, 4, 1, 2, 3, 4]

You can't multiply arrays in JavaScript. Your current solution using Array.concat is correct. Note that it creates a separate array with the copied items, and does not alter the original.