我有两个数组['b','c','d','e']& [ 'H', 'I', 'K', 'L', 'M', 'N'] 我想使用Javascript / AngularJS程序找到上面数组的长度为2的组合 e.g
['bh','bi','bk','bk','bl','bm','bn','ch,'ci','ck'......]
由于
答案 0 :(得分:0)
这是功能编程 ES6解决方案:
var array = ['b','c','d','e'];
var array2 = ['h','i','k','l','m','n'];
var result = array.reduce( (a, v) =>
[...a, ...array2.map(x=>v+x)],
[]);
console.log(result);
/*---------OR--------------*/
var result1 = array.reduce( (a, v, i) =>
a.concat(array2.map( w => v + w )),
[]);
console.log(result1);
/*-------------OR(without arrow function)---------------*/
var result2 = array.reduce(function(a, v, i) {
a = a.concat(array2.map(function(w){
return v + w
}));
return a;
},[]
);
console.log(result2);
/*----------for single array------------*/
var result4 = array.reduce( (a, v, i) =>
[...a, ...array.slice(i+1).map(x=>v+'-'+x)],
[]);
console.log(result4);
答案 1 :(得分:0)
使用嵌套Array.map()
来电并组合这些字母。将数组按spreading展平为Array.concat()
:
const arr1 = ['b','c','d','e'];
const arr2 = ['h','i','k','l','m','n'];
const result = [].concat(...arr1.map((l1) => arr2.map((l2) => l1 + l2)));
console.log(result);