# vue2 多个数组如何按照要求合并成一个数组？

``````a = [1,2,3,4,5]
b = [a,b,c]
c = [A,Z]``````

``mix = ['1aA','1aZ','1bA','1bZ','1cA','1cZ','2aA','2aZ'..........]``

4 个回答
✓ 已被采纳

``````  const a = [1, 2, 3, 4, 5];
const b = ['a', 'b', 'c'];
const c = ['A', 'Z'];
const result = [];
for (let aItem of a) {
for (let bItem of b) {
for (let cItem of c) {
result.push('' + aItem + bItem + cItem);
}
}
}
console.log(result);``````

``````const demo=function(...args) {
const result = [];
const arrays = args.filter(arg => arg.length);
build(arrays, 0, '', result);
console.log(result);
};
const build=function (arrays, index, str, result) {
const array = arrays[index++];
if (!array) {
result.push(str);
return;
}
for (let item of array) {
build(arrays, index, str + item, result);
}
}``````

``````function combo(...arrays) {
if (!arrays.length) { return []; }
const [current, ...rest] = arrays;
return rest.length
? combo(...rest).flatMap(r => current.map(c => `\${c}\${r}`))
: current;
}

// 调用示例
console.log(combo(a, b, c));``````

``````function combo(...arrays) {
return arrays.reduce(
(r, row) => r.flatMap(it => row.map(c => `\${c}\${it}`)),
[""]
);
}

console.log(combo(a, b, c));``````

``````// 首先计算[1,2,3,4,5] * ['a','b','c']，得到结果[1a, 1b, 1c, 2a, 2b, 2c...]
// 然后计算[1a, 1b, 1c, 2a, 2b, 2c...] * [A, Z]，得到最终结果[1aA, 1aZ, 1bA, 1bZ, 1cA, 1cZ...]
let a = [1,2,3,4,5]
let b = ['a','b','c']
let c = ['A','Z']
let d = ['q','w']
function permutation (val) {
return val.reduce((prev, cur) => {
let arr = []
prev.forEach(v => {
cur.forEach(it => {
arr.push(`\${v}\${it}`)
})
})
return arr
})
}
console.log(permutation([a, b]))
console.log(permutation([a, b, c]))
console.log(permutation([a, b, c, d]))``````

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进