合并数组数组

Posted

技术标签:

【中文标题】合并数组数组【英文标题】:Merging array of arrays 【发布时间】:2022-01-22 11:09:38 【问题描述】:

我要解决的问题如下

我们确实有一个数组

const array = [[1, 2, 3], ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"],["+", "-", "*", "/", "?"]];

我们想要这样的输出:

const output = [[1,"A","+"],[1,"A","-"],[1,"A","*"],[1,"A","/"],[1,"A","?"],[1,"B","+"],[1,"B","-"],[1,"B","*"],[1,"B","/"],[1,"B","?"],[1,"C","+"],[1,"C","-"],[1,"C","*"],[1,"C","/"],[1,"C","?"],[1,"D","+"],[1,"D","-"],[1,"D","*"],[1,"D","/"],[1,"D","?"],[1,"E","+"],[1,"E","-"],[1,"E","*"],[1,"E","/"],[1,"E","?"],[1,"F","+"],[1,"F","-"],[1,"F","*"],[1,"F","/"],[1,"F","?"],[1,"G","+"],[1,"G","-"],[1,"G","*"],[1,"G","/"],[1,"G","?"],[1,"H","+"],[1,"H","-"],[1,"H","*"],[1,"H","/"],[1,"H","?"],[1,"I","+"],[1,"I","-"],[1,"I","*"],[1,"I","/"],[1,"I","?"],[1,"J","+"],[1,"J","-"],[1,"J","*"],[1,"J","/"],[1,"J","?"],[2,"A","+"],[2,"A","-"],[2,"A","*"],[2,"A","/"],[2,"A","?"],[2,"B","+"],[2,"B","-"],[2,"B","*"],[2,"B","/"],[2,"B","?"],[2,"C","+"],[2,"C","-"],[2,"C","*"],[2,"C","/"],[2,"C","?"],[2,"D","+"],[2,"D","-"],[2,"D","*"],[2,"D","/"],[2,"D","?"],[2,"E","+"],[2,"E","-"],[2,"E","*"],[2,"E","/"],[2,"E","?"],[2,"F","+"],[2,"F","-"],[2,"F","*"],[2,"F","/"],[2,"F","?"],[2,"G","+"],[2,"G","-"],[2,"G","*"],[2,"G","/"],[2,"G","?"],[2,"H","+"],[2,"H","-"],[2,"H","*"],[2,"H","/"],[2,"H","?"],[2,"I","+"],[2,"I","-"],[2,"I","*"],[2,"I","/"],[2,"I","?"],[2,"J","+"],[2,"J","-"],[2,"J","*"],[2,"J","/"],[2,"J","?"],[3,"A","+"],[3,"A","-"],[3,"A","*"],[3,"A","/"],[3,"A","?"],[3,"B","+"],[3,"B","-"],[3,"B","*"],[3,"B","/"],[3,"B","?"],[3,"C","+"],[3,"C","-"],[3,"C","*"],[3,"C","/"],[3,"C","?"],[3,"D","+"],[3,"D","-"],[3,"D","*"],[3,"D","/"],[3,"D","?"],[3,"E","+"],[3,"E","-"],[3,"E","*"],[3,"E","/"],[3,"E","?"],[3,"F","+"],[3,"F","-"],[3,"F","*"],[3,"F","/"],[3,"F","?"],[3,"G","+"],[3,"G","-"],[3,"G","*"],[3,"G","/"],[3,"G","?"],[3,"H","+"],[3,"H","-"],[3,"H","*"],[3,"H","/"],[3,"H","?"],[3,"I","+"],[3,"I","-"],[3,"I","*"],[3,"I","/"],[3,"I","?"],[3,"J","+"],[3,"J","-"],[3,"J","*"],[3,"J","/"],[3,"J","?"]]

我们不知道父 Array 的尺寸,而孩子可以有各种尺寸和类型

【问题讨论】:

另见:How do I zip two arrays in javascript? 尝试了多个子平面映射 -> ``` a1.flatMap((a) => a2.flatMap((b) => a3.flatMap((c) => a4.flatMap(( d) => a5.map((e) => [a, b, c, d, e]))) ) ``` 现在你已经编辑了它,它看起来只是一个组合问题。 All possible combinations of a 2d array in Javascript @pilchard。谢谢。它奏效了 【参考方案1】:

@pilchard 指出了这个解决方案:All possible combinations of a 2d array in Javascript

function combos(list, n = 0, result = [], current = [])
    if (n === list.length) result.push(current)
    else list[n].forEach(item => combos(list, n+1, result, [...current, item]))
 
    return result

我可以确认它有效

【讨论】:

【参考方案2】:

您可以使用内部地图:

const array = [
  [1, 2, 3],
  ['A', 'B', 'C'],
  ['+', '-', '*']
];

const res = array.map((e, i) => array.map(f => f[i]))
console.log(res)

【讨论】:

这是个好方法。但是,如果元素具有不同的大小,它会失败数组的大小:``` const arrays = [ [1, 2, 3,4,5,6,7,8,9,10], ['A', 'B ', 'C', 'D', 'E'], ['+'] ]; ``` @Andrew 如果数组大小不同,我不清楚您期望结果是什么。 @spectric。这将是一个很好的例子。试试这个作为输入 ``` const array = [ [1, 2, 3], ['A', 'B', 'C', 'D', 'E','F', 'G',' H','I', 'J'], ['+', '-', '*', '/', '?'] ]; ```无论如何感谢您的帮助。 @Andrew 该数组的预期结果是什么? 150 个数组:``` [[1,"A","+"],[1,"A","-"],[1,"A"," "],[1,"A","/"],[1,"A","?"],[1,"B","+"],[1,"B","-"] ,[1,"B",""],[1,"B","/"],[1,"B","?"],[1,"C","+" ],[1,"C","-"],[1,"C",""],[1,"C","/"],[1,"C","?" ],[1,"D","+"],[1,"D","-"],[1,"D",""],[1,"D","/ "],[1,"D","?"],[1,"E","+"],[1,"E","-"],[1,"E"," "],[1,"E","/"],[1,"E","?"],[1,"F","+"],[1,"F","-"] ,[1,"F",""],[1,"F","/"],[1,"F","?"],[1,"G","+" ],[1,"G","-"],[1,"G",""],[1,"G","/"],[1,"G","?" ],[1,"H","+"],[1,"H","-"],[1,"H",""],[1,"H","/ "],[1,"H","?"],[1,"I","+"],[1,"I","-"],[1,"I","*"] ,[1,"I","/"],[1,"I","?"],....```

以上是关于合并数组数组的主要内容,如果未能解决你的问题,请参考以下文章

java 合并排序数组 - 合并两个排序的数组

java数组的合并问题

C语言合并两个数组

java String数组合并

两个数组交叉合并为一个数组

JAVA中的数组合并问题