The problem I am trying to solve is that I want to generate an array which represents all combinations of elements in a source array.
Given an input of 2 items, there are 4 combinations (represented here as binary)
Group 1 | 0 | 1 | 0 | 1
Group 2 | 0 | 0 | 1 | 1
--------------------------
&Result | 0 | 1 | 2 | 3
This can be generalised so the number of combinations is 2(number of groups) (So 3 groups has 8 combinations, 4 has 16 etc).
So the question is; given a javascript array:
var groups = [
{
name:"group1",
bit: 1
},
{
name:"group2",
bit: 2
},
{
name:"group3",
bit: 4
}];
I need to generate an array where the index represents the and'ing of the bit
property and the value of the array is arbitrary (further calculation - not relevant) so lets just make it an array of the group names (for the purpose of this question). This result is desirable:
var result = [
{groups: []}, //0
{groups: ["group1"]}, //1
{groups: ["group2"]}, //2
{groups: ["group1","group2"]}, //3
{groups: ["group3"]}, //4
{groups: ["group1","group3"]}, //5
{groups: ["group2","group3"]}, //6
{groups: ["group1","group2","group3"]} //7
]
You can see in the comments there that each index in the array represents the act of and'ing the bit
property from the original.
I have prepared a jsfiddle with the input and required output should it be useful in answering the question.
This is my current solution, based on mellamokb's answer but re-written in my prefered style. I was hoping there was a more elegant solution as this has a lot of iterations over the array which are unecessary. Any better solutions?
var resultCount = Math.pow(2,groups.length);
var result = [];
for(var i=0;i<resultCount;i++){
result.push({
groups: $.map(groups, function(e,idx){
return ((i & Math.pow(2,idx)) != 0)
? e.name
: null
})
});
}