I have an 'item' object in JavaScript, and the item can have settings like color, size, etc.
I need to get all possible combinations in an array.
So lets say we have an item that looks like this:
var newItem = {
name: 'new item',
Settings: [
{name: 'color', values: ['green', 'blue', 'red']},
{name: 'size', values: ['15', '18', '22']},
{name: 'gender',values: ['male', 'female']}
]
};
I need to somehow get this:
[
[{SettingName:'color',value:'green'},{SettingName:'size',value:'15'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'blue'},{SettingName:'size',value:'15'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'red'},{SettingName:'size',value:'15'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'green'},{SettingName:'size',value:'18'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'blue'},{SettingName:'size',value:'18'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'red'},{SettingName:'size',value:'18'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'green'},{SettingName:'size',value:'22'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'blue'},{SettingName:'size',value:'22'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'red'},{SettingName:'size',value:'22'},{SettingName:'gender',value:'male'}],
[{SettingName:'color',value:'green'},{SettingName:'size',value:'15'},{SettingName:'gender',value:'female'}],
[{SettingName:'color',value:'blue'},{SettingName:'size',value:'15'},{SettingName:'gender',value:'female'}],
[{SettingName:'color',value:'red'},{SettingName:'size',value:'15'},{SettingName:'gender',value:'female'}],
[{SettingName:'color',value:'green'},{SettingName:'size',value:'18'},{SettingName:'gender',value:'female'}],
[{SettingName:'color',value:'blue'},{SettingName:'size',value:'18'},{SettingName:'gender',value:'female'}],
[{SettingName:'color',value:'red'},{SettingName:'size',value:'18'},{SettingName:'gender',value:'female'}],
[{SettingName:'color',value:'green'},{SettingName:'size',value:'22'},{SettingName:'gender',value:'female'}],
[{SettingName:'color',value:'blue'},{SettingName:'size',value:'22'},{SettingName:'gender',value:'female'}],
[{SettingName:'color',value:'red'},{SettingName:'size',value:'22'},{SettingName:'gender',value:'female'}]
]
You can use
Array.prototype.map()
,for
loop,while
loop,Array.prototype.concat()
. Iterategender
values; select each ofcolor
,size
value in succession beginning at index0
of either; iterating the furthest adjacent array from currentgender
, increment the index of the closest adjacent array; merge the resulting twogender
arrays to form a single array containing all combinations ofgender
,color
,size
plnkr http://plnkr.co/edit/C2fOJpfwOrlBwHLQ2izh?p=preview
This can be a good interview question.
See JS Bin for running example.
Here is a none recursive solution. It takes an empty or existing
settings
"matrix" and avalues
array, and return a new matrix as a combination of existing matrix content cloned for each new value, appended with pairs of new value setting items.[A]
->[1,2]
gives[A][1][A][2]
[A][1][A][2]
->[X,Y]
gives[A][1][X][A][2][Y][A][2][X][A][1][Y]
and so on
You can now create the desired settings literal this way :
demo -> http://jsfiddle.net/b4ck98mf/
The above produces this :
An approach using
Array.prototype.reduce()
,Array.prototype.sort()
,Object.keys()
,for
loop,while
loop