简体   繁体   中英

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].

How can I create third array like this c = [[1, 2, 3, x], [1, 2, 3, y], [1, 2, 3, z]] from them?

Thanks.

You can use map() method on b and then concat() each element on 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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