0

I'm trying to make an combination with an array of arrays in javascript. The point is the length of given array can be any number. For example, input and output are like this. Input:

[
  [{
    level: 1,
    value: "a"
  }, {
    level: 1,
    value: "b"
  }],
  [{
    level: 2,
    value: "c"
  }, {
    level: 2,
    value: "d"
  }],
  [{
    level: 3,
    value: "e"
  }, {
    level: 3,
    value: "f"
  }]
]

Expected Output:

[
  [{
    level: 1,
    value: "a"
  }, {
    level: 2,
    value: "c"
  }, {
    level: 3,
    value: "e"
  }],
  [{
    level: 1,
    value: "a"
  }, {
    level: 2,
    value: "c"
  }, {
    level: 3,
    value: "f"
  }],
  [{
    level: 1,
    value: "a"
  }, {
    level: 2,
    value: "d"
  }, {
    level: 3,
    value: "e"
  }],
  [{
    level: 1,
    value: "a"
  }, {
    level: 2,
    value: "d"
  }, {
    level: 3,
    value: "f"
  }]
  [{
    level: 1,
    value: "b"
  }, {
    level: 2,
    value: "c"
  }, {
    level: 3,
    value: "e"
  }],
  [{
    level: 1,
    value: "b"
  }, {
    level: 2,
    value: "c"
  }, {
    level: 3,
    value: "f"
  }],
  [{
    level: 1,
    value: "b"
  }, {
    level: 2,
    value: "d"
  }, {
    level: 3,
    value: "e"
  }],
  [{
    level: 1,
    value: "b"
  }, {
    level: 2,
    value: "d"
  }, {
    level: 3,
    value: "f"
  }]
]

The temporary solution is as below and I would like to shorten this function using a recursive function call.

options = []

for (const level1 of levels[0]) {
    for (const level2 of levels[1] {
        for (const level3 of levels[2] {
            options.push([level1, level2, level3])
        }
    } 
}

This function above shoule be shorten as follwing.

const result = getCombinations(array, array.length);
function getCombinations(array, length) {
 let result = [];
function iter(array) {
 // do something here 
}
 let i = 0; 
 while(i < length)
  iter(array[i++], length);
 }

It doesn't only have to be the same way as above.

I'm trying not to hard-code depending on the length of array as the example for the repeated for-loop code using recursive function.

opendev
  • 29
  • 6
  • 1
    Please explain the logic because the pattern doesn't make any sense. – zer00ne Oct 09 '22 at 17:05
  • you could take a cartesian product, like [this answer](https://stackoverflow.com/a/44012184/1447675), which uses a generator function. – Nina Scholz Oct 09 '22 at 17:10
  • So basically, input has to come out as output in a new array. – opendev Oct 09 '22 at 17:11
  • please still explain _what_ you think is happening here, because turning `{{a,b}, {c,d}, {e,f}}` into `{{a,c,e}, {a,c,f}, {a,d,e}, {a,d,f}, {b,c,e}, {b,c,f}, {b,d,e}, {b,d,f}}` is basically just "get all permutations without reordering". The object stuff around the values is effectively irrelevant, the level is already implicit to the the value's position in the resulting sets. (plenty of tutorials for that, even in JS, if you search the web for those terms) – Mike 'Pomax' Kamermans Oct 09 '22 at 18:30
  • @Mike'Pomax'Kamermans yes, it's already set in order by level. And the each level is the process of order users go through as a survey. I'm trying to make the all the possible result set of answers from the users. – opendev Oct 10 '22 at 00:15
  • So are you trying to permute _this_ data, or do you have _different, real_ data to permute? (if so, [please show _that_ data](/help/how-to-ask)). Because for _this_ data, you can just use a generator and not bother with any level/value properties, just run the strings. – Mike 'Pomax' Kamermans Oct 10 '22 at 01:30
  • @Mike'Pomax'Kamermans I have different, real data but the form is the pretty much same as the data. For instnce, question types and options mapping to the types and I need all the possible mapping datas – opendev Oct 10 '22 at 01:32
  • @Mike'Pomax'Kamermans How do you use a generator in that case? – opendev Oct 10 '22 at 01:34
  • without showing that actual data in your post, hard to tell. It would be a great idea to update your post to show a real world example of the data you're handling. – Mike 'Pomax' Kamermans Oct 10 '22 at 16:17

0 Answers0