简体   繁体   中英

Combine each of the values from one array with each of the values in another array

Is there anything that can take two arrays and combine each of their respective values? And the arrays are not the same length. Like this:

array1 = ["one", "two", "three"];
array2 = ["twenty", "thirty", "forty", "fifty"]

arrayWant= ["twentyone", "twentytwo", "twentythree", "thirtyone", "thirtytwo", "thirtythree", "fortyone", "fortytwo", "fortythree"...]

there is no equivalent in javascript afaict. But you can write yourself.

 let array1 = ["one", "two", "three"]; let array2 = ["twenty", "thirty", "forty", "fifty"] let arrayWant= array2.map(x=>array1.map(y=>x+y)).flat() console.log(arrayWant)


also, Array.forEach would return undefined , so you should use map for this.

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