复制数组任意次数(javascript) [英] Duplicate an array an arbitrary number of times (javascript)

查看:84
本文介绍了复制数组任意次数(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个阵列。这个数组的长度是3,有3个元素:

Let's say I'm given an array. The length of this array is 3, and has 3 elements:

var array = ['1','2','3'];

最后我需要检查这个数组是否等于具有相同元素的数组,但只是现在两次。我的新数组是:

Eventually I will need to check if this array is equal to an array with the same elements, but just twice now. My new array is:

var newArray = ['1','2','3','1','2','3'];

我知道我可以使用array.splice()复制数组,但是如何复制它未知的次数?基本上我想要的东西会产生影响

I know I can use array.splice() to duplicate an array, but how can I duplicate it an unknown amount of times? Basically what I want is something that would have the effect of

var dupeArray = array*2;


推荐答案

这是一种相当简洁,非递归的复制方式一个数组任意次:

Here's a fairly concise, non-recursive way of replicating an array an arbitrary number of times:

function replicateArray(array, n) {
  // Create an array of size "n" with undefined values
  var arrays = Array.apply(null, new Array(n)); 

  // Replace each "undefined" with our array, resulting in an array of n copies of our array
  arrays = arrays.map(function() { return array });

  // Flatten our array of arrays
  return [].concat.apply([], arrays);
}

console.log(replicateArray([1,2,3],4)); // output: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

发生了什么?

前两行使用 apply map 创建数组的n个副本数组。

The first two lines use apply and map to create an array of "n" copies of your array.

最后一行使用应用 展平我们最近生成的数组数组。

The last line uses apply to flatten our recently generated array of arrays.

严重的是,发生了什么?

如果你还没有使用过apply或map,代码可能会令人困惑。

If you haven't used apply or map, the code might be confusing.

这里的第一块魔术酱是使用 apply() 这使e成为可能ither将数组传递给函数,好像它是一个参数列表。

The first piece of magic sauce here is the use of apply() which makes it possible to either pass an array to a function as though it were a parameter list.

Apply使用三条信息: x.apply(y,z )

Apply uses three pieces of information: x.apply(y,z)


  • x是被调用的函数

  • y是正在调用函数的对象(如果 null ,它使用全局

  • z是参数列表

  • x is the function being called
  • y is the object that the function is being called on (if null, it uses global)
  • z is the parameter list

根据代码,它转换为: yx(z [0],z [1],z [2],...)

Put in terms of code, it translates to: y.x(z[0], z[1], z[2],...)

例如

var arrays = Array.apply(null, new Array(n));

与写作相同

var arrays = Array(undefined,undefined,undefined,... /*Repeat N Times*/);






第二块魔法就是使用< a href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map =nofollow noreferrer> map() ,它为数组的每个元素调用一个函数,并创建一个返回值列表。


The second piece of magic is the use of map() which calls a function for each element of an array and creates a list of return values.

这使用两条信息: x.map(y)


  • x是一个数组

  • y是要在数组的每个元素上调用的函数

例如

var returnArray = [1,2,3].map(function(x) {return x + 1;});

会创建数组[2,3,4]

would create the array [2,3,4]

在我们的例子中,我们传入一个函数,该函数总是返回一个静态值(我们想要复制的数组),这意味着这个映射的结果是我们数组的n个副本的列表。

In our case we passed in a function which always returns a static value (the array we want to duplicate) which means the result of this map is a list of n copies of our array.

这篇关于复制数组任意次数(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆