简体   繁体   中英

Javascript - How to concatenate string values from two arrays of different sizes?

I have two arrays, for example:

Array1:

arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"]

Array2:

arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"]

I want to generate a new array from two above concatenating the individual items into new items recursively for each one in array1, to get a final array like this:

final = ['Precon-EJID', 'Contra-EJID', 'Postco-EJID', 'Cancel-EJID', 'Consul-EJID', 'Precon-EMBA', 'Contra-EMBA', 'Postco-EMBA', 'Cancel-EMBA', 'Consul-EMBA', 'Precon-EMPR', 'Contra-EMPR', 'Postco-EMPR', 'Cancel-EMPR', 'Consul-EMPR'...etc]

Thank you in advance

You can do this with 2 simple for of loops:

 var arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"]; var arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"] var finalArr = []; for ( var item2 of arr2 ) { for ( var item1 of arr1 ) { finalArr.push(`${item1}-${item2}`); } } console.log(finalArr); 

You can use nested Array#map calls, and flatten the results using Array#concat :

 const arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"] const arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"] const result = [].concat(...arr2.map((s1) => arr1.map((s2) => `${s2}-${s1}`))) console.log(result) 

Here's a one liner to do this:

 arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"] arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"] const result = [].concat(...arr1.map(prefix => arr2.map(suffix => prefix+suffix))); console.log(result) // EDIT: if order matters, reverse the use of arr1 and arr2: const orderedResult = [].concat(...arr2.map(suffix => arr1.map(prefix => prefix+suffix))); console.log(orderedResult) 

 const prefixes = [ "Precon", "Contra", "Postco", "Cancel", "Consul" ]; const suffixes = [ "EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999" ]; const results = suffixes .map(suffix => prefixes.map(prefix => prefix + '-' + suffix)) .reduce((xs, ys) => [...xs, ...ys]); console.log(results); 

Another option is to use reduce...

 let arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"] let arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"] let arr3 = arr2.reduce((arr, val) => { let f = [] arr1.forEach(itm => val && f.push(itm + '-' + val)) return arr.concat(f) }, []) console.log(arr3) 

Here's a recursive one. Just like the OP asked. Took me a while. :P

 arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"] arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"] var arr = []; console.log(concat(arr1, arr2, 0, 0, 0)); function concat(arr1, arr2, arrIndex, index1, index2) { //console.log(arr1.length); if (index2 === (arr2.length)) { return; } else { arr[arrIndex] = arr1[index1] + '-' + arr2[index2]; if (index1 !== (arr1.length - 1)) { index1++; } else if (index1 === (arr1.length - 1)) { index1 = 0; index2++; //concat(arr1, arr2, ++arrIndex, index1, index2++); // Not here dummy :P } concat(arr1, arr2, ++arrIndex, index1, index2++); } return arr; } 

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