0

I have n number of arrays structured like

[{val_1: 2}, {val_1: 3}];
[{val_2: 2}];
[{val_3: 1}];

etc.

I want to create a single array structured like

[{val_1: 2, val_2: 2, val_3: 1}, {val_1: 3, val_2: 2, val_3: 1}]

in which all arrays are compared and every single possible output is produced. In the below code I have 3 arrays. One with 4 values, 2 values, and 3 values. I want it to return 24 different arrays showing all possible combinations.

      var val_1 = 3
  var val_2 = 2
  var val_3 = 1
  var total_val = 3;
  var useable = 50;

  var array_1 = new Array();
  var array_2 = new Array();
  var array_3 = new Array();
  var array_1_c = new Array();
  var array_2_c = new Array();
  var array_3_c = new Array();

  for(val_1; val_1 <= total_val && val_1 <= useable; val_1+=1){
    array_1 = [{val_1}];
    array_1.map(test =>{
      return{val_1}
    }).forEach(test => array_1_c.push(test));
  };
  var val_1 = 2
  for(val_1; val_1 >= 0; val_1-=1){
    array_1 = [{val_1}];
    array_1.map(test =>{
      return{val_1}
    }).forEach(test => array_1_c.push(test));
  };

  for(val_2; val_2 <= total_val && val_2 <= useable; val_2+=1){
    array_2 = [{val_2}];
    array_2.map(test =>{
      return{val_2}
    }).forEach(test => array_2_c.push(test));
  };
  for(val_3; val_3 <= total_val && val_3 <= useable; val_3+=1){
    array_3 = [{val_3}];
    array_3.map(test =>{
      return{val_3}
    }).forEach(test => array_3_c.push(test));
  }; console.log(array_1_c);console.log(array_2_c);console.log(array_3_c);

how would something like this be accomplished? The permute function seems to be the way to go, but I can't find a way to output every single combination. If creating multiple million combinations should I go a different route than using an object array.

Thanks!

4
  • Hi, can you please explain how are you evaluating 12 combinations from arrays of sizes 4, 2 & 3? Commented Apr 21, 2020 at 17:15
  • Not sure why this is downvoted. Commented Apr 21, 2020 at 17:24
  • Sorry I meant 24 combinations*. 4 * 2 * 3 = 24. The result needs to be a combination of each value in order. Commented Apr 21, 2020 at 17:32
  • I guess you need a solution of Cartesian product, not permutations. Take a look at: stackoverflow.com/questions/56972012/… This question have a relevant answer to your problem. Commented Apr 21, 2020 at 18:05

1 Answer 1

2

You could take an algorithm for a cartesian product by using arrays of objects.

The result is an array of object with all properties of the given data.

const
    getCartesian = array => array.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => ({ ...v, ...w }))), [])),
    all = [[{ val_1: 2 }, { val_1: 3 }], [{ val_2: 2 }], [{ val_3: 1 }]],
    cartesian = getCartesian(all);
  
console.log(cartesian);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.