简体   繁体   English

复制数组并将一个元素从另一数组推入每个副本

[英]Copy array and push one element from another array to every copy

I have two arrays, let's say a = [1, 2, 3] and b = [x, y, z]. 我有两个数组,假设a = [1, 2, 3]b = [x, y, z].

How can I create third array like this c = [[1, 2, 3, x], [1, 2, 3, y], [1, 2, 3, z]] from them? 我如何从它们创建像c = [[1, 2, 3, x], [1, 2, 3, y], [1, 2, 3, z]]第三个数组?

Thanks. 谢谢。

You can use map() method on b and then concat() each element on a . 可以使用map()方法在b和然后concat()上的每个元件a

 var a = [1, 2, 3], b = ['x', 'y', 'z'] var result = b.map(e => a.concat(e)); console.log(result) 

You can use .map() to do this, like so: 您可以使用.map()执行此操作,如下所示:

 var a = [1,2,3]; var b = ["x", "y", "z"]; var c = b.map(function (elem) { return a.concat(elem); }); console.log(c); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM