-10

The problem, I'm trying to solve is as followed

we do have an array

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

we want to have an output like this:

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","?"]]

We dont't know the size of the parent Array and children can have various sizes and types

Andrew
  • 1
  • 1
  • also see: [How do I zip two arrays in JavaScript?](https://stackoverflow.com/questions/22015684/how-do-i-zip-two-arrays-in-javascript) – pilchard Dec 20 '21 at 22:10
  • Tried multiple flatMapping of children -> ``` a1.flatMap((a) => a2.flatMap((b) => a3.flatMap((c) => a4.flatMap((d) => a5.map((e) => [a, b, c, d, e]))) ) ``` – Andrew Dec 20 '21 at 23:30
  • Now that you've edited it appears it's just a combinations question. [All possible combinations of a 2d array in Javascript](https://stackoverflow.com/questions/53311809/all-possible-combinations-of-a-2d-array-in-javascript) – pilchard Dec 21 '21 at 00:52
  • @pilchard. Thanks. it worked – Andrew Dec 21 '21 at 03:08

3 Answers3

0

@pilchard has point out this solution: 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
}

I can confirm that it works

Andrew
  • 1
  • 1
0

@Andrew I tried this approach let me know if it will work.

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

let result;
let innerArray;
innerArray = array[0].map(a1 =>
    array[1].map(a2 =>
        array[2].map(a3 => [a1, a2, a3])
    )

)
result = innerArray.flat(2)

console.log("Result", result)
saisuresh
  • 71
  • 1
  • 8
-1

You can use an inner map:

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

const res = array.map((e, i) => array.map(f => f[i]))
console.log(res)
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • It good approach. however it fails the size of the array if elements have different sizes: ``` const arrays = [ [1, 2, 3,4,5,6,7,8,9,10], ['A', 'B', 'C', 'D', 'E'], ['+'] ]; ``` – Andrew Dec 20 '21 at 23:21
  • @Andrew It's not clear to me what you would expect the result to be then, if the arrays are different sizes. – Spectric Dec 21 '21 at 00:06
  • @spectric. this would be a good example. try this as an input ``` const array = [ [1, 2, 3], ['A', 'B', 'C', 'D', 'E','F', 'G','H','I', 'J'], ['+', '-', '*', '/', '?'] ]; ``` Thanks for the help anyway. – Andrew Dec 21 '21 at 00:09
  • @Andrew What is the expected result with that array? – Spectric Dec 21 '21 at 00:09
  • 150 arrays: ``` [[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","?"],.... ``` – Andrew Dec 21 '21 at 00:22
  • @spectric I have updated the question to reflect the question and answer to the problem – Andrew Dec 21 '21 at 00:28