简体   繁体   English

Javascript-如何从不同大小的两个数组中连接字符串值?

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

I have two arrays, for example: 我有两个数组,例如:

Array1: 数组1:

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

Array2: 数组2:

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: 我想从上面的两个中生成一个新数组,对于array1中的每个项目,将各个项目递归连接到新项目中,以获得最终的数组,如下所示:

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: 您可以使用2个简单的for of循环来做到这一点:

 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 : 您可以使用嵌套的Array#map调用,并使用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... 另一种选择是使用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. 就像OP问的那样。 Took me a while. 花了我一段时间。 :P :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; } 

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

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