Attempting to transform an infinite number of arrays of objects into a matrix.
height: [1,3,4,5,6,7]
weight: [23,30,40,50,90,100]
to
1 23
1 30
1 40
1 50
...
3 23
3 30
3 40
...
Essentially mapping out all possible combinations into a matrix
I experimented with solving the issue using various functions in underscore.js
var firstOption = _.first( productOptionKeys );
$.each( productOptions[ firstOption ].split(","), function(index, value){
var matrixRow = [];
var matricableOptionKeys = _.reject( productOptionKeys, function(option){ return (option == firstOption); } );
matrixRow.push( value );
$.each( matricableOptionKeys, function( index, value ){
var matricableOptions = productOptions[ value ].split(",");
var matricIndex = 0;
for( i=0 ; i<matricableOptions.length; i++ ){
matrixRow.push( matricableOptions[ matricIndex ] );
}
//matricIndex ++;
// $.each( productOptions[ value ].split(","), function(index, value){
// matrixRow.push( value );
// return false;
// });
});
console.log( matrixRow );
});
The object is a bit more complex in my scenario as I need to split the entered string values into an array. However, for simplicity in finding a solution, I omitted that these values are comma-separated strings.
This question is unique as it aims to get all permutations of an object like the one below
Object { Height: "23,45,190,800", Width: "11",14",15",20"", Color: "Red,Green,Blue" }
Current solutions only cover permutations for arrays, not for objects. The values for each key will always be separated by commas, so `object[key].split(",")` will provide the values as an array.
If it were feasible to concatenate arguments for function calls, one of the existing functions could have been utilized.