Trying to convert an infinite number of object arrays into a matrix.
height: [1,3,4,5,6,7]
weight: [23,30,40,50,90,100]
into
1 23
1 30
1 40
1 50
...
3 23
3 30
3 40
...
Basically map out all possible bination into a matrix
I tried solving the problem using a few 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 slightly more plicated in my case since I have to split the string values entered into array. But to ease ing up with solution I omitted that the values are ma separated strings
This is not a duplicate of other similar questions, cause am trying obtain all permutations of an object like on below
Object { Height: "23,45,190,800", Width: "11",14",15",20"", Color: "Red,Green,Blue" }
the existing solutions only find permutations for arrays, not an object. the values of each key will always be ma separated, so object[ key ].split(",") will return values as array.
If it were possible to concatenate arguments to call function I could make use of one of the existing functions.
Trying to convert an infinite number of object arrays into a matrix.
height: [1,3,4,5,6,7]
weight: [23,30,40,50,90,100]
into
1 23
1 30
1 40
1 50
...
3 23
3 30
3 40
...
Basically map out all possible bination into a matrix
I tried solving the problem using a few 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 slightly more plicated in my case since I have to split the string values entered into array. But to ease ing up with solution I omitted that the values are ma separated strings
This is not a duplicate of other similar questions, cause am trying obtain all permutations of an object like on below
Object { Height: "23,45,190,800", Width: "11",14",15",20"", Color: "Red,Green,Blue" }
the existing solutions only find permutations for arrays, not an object. the values of each key will always be ma separated, so object[ key ].split(",") will return values as array.
If it were possible to concatenate arguments to call function I could make use of one of the existing functions.
Share Improve this question edited Jan 12, 2015 at 19:14 isawk asked Jan 12, 2015 at 13:20 isawkisawk 1,05710 silver badges22 bronze badges 6- 1 You have to show an attempt, you shouldn't just ask for others to write the code for you – Ruan Mendes Commented Jan 12, 2015 at 13:22
- where is the code you have tried? – atmd Commented Jan 12, 2015 at 13:49
- "Trying to convert an infinite number of object arrays". If it's infinite, I'm afraid you won't be able to do it in a finite time. – Oriol Commented Jan 12, 2015 at 15:36
- @Oriol not exactly infinite, I guess I was trying to municate that one would specify more dimensions – isawk Commented Jan 12, 2015 at 18:31
- @JuanMendes didn't think it was necessary to show my attempt. But understand, this is not a code for me forum – isawk Commented Jan 12, 2015 at 18:33
2 Answers
Reset to default 6Sure thing, you could use a double for:
var result = [];
for(var i=0; i<height.length; i++)
for(var j=0; j<weight.length; j++)
result.push({ height: height[i], weight: weight[j]});
The array called result
at the end will contain all the possible binations of height
and weight
.
Another way it would be you use the forEach
method of array:
var result = [];
height.forEach(function(h){
weight.forEach(function(w){
result.push({ height: h, weight: w});
});
});
try this
var height = [1,3,4,5,6,7];
var weight = [23,30,40,50,90,100];
var res = [];
for(h = 0; h < height.length; h++) {
for(w = 0; w < weight.length; w++) {
res.push({h: height[h], w: weight[w]});
}
}