-1

I've tried various approaches, basically, if I have a red and a green option as one set, and another set of sizes, say small and large, I would need the following to be true:

const input = [{
    label : 'Colours',
    options: [{text : 'red'}, {text : 'green'}]
},{
    label : 'Sizes',
    options: [{text : 'small'}, {text: 'large'}]
}];

I would expect this output:

red, small,
red, large,
green, small, 
green, large

Below is a more complicated but similar structure, which should also be able to generate from the same code:

[{"label":"Fitting Options","options":[{"text":"No Thanks","value":"no-thanks","price":0},{"text":"Yes Please (+$110.00 )","value":"yes-please","price":"110.00"}]},{"label":"Memory Card Size","options":[{"text":"128GB (+$180.00 )","value":"128gb","price":"180.00"},{"text":"16GB","value":"16gb","price":0},{"text":"32GB (+$30.00 )","value":"32gb","price":"30.00"},{"text":"64GB (+$80.00 )","value":"64gb","price":"80.00"}]}]

I used this as reference but can't quite reverse engineer it! Get all variants from a multidimensional array in javascript

Shannon Hochkins
  • 10,888
  • 14
  • 56
  • 90
  • 2
    What is the actual code you have used so far so we can see where your issue is? – basic Feb 03 '19 at 04:16
  • My code is useless, I haven't even come close to the solution! @basic the solution on the page i referenced, is what i need, but the data structure is also very different! – Shannon Hochkins Feb 03 '19 at 04:19
  • @Shannon Hochkins what is your expected output for the first array `input` – Maheer Ali Feb 03 '19 at 04:29
  • Why do you need to _"reverse engineer"_ the code at the answers at the linked questions? – guest271314 Feb 03 '19 at 05:11
  • So wait @ShannonHochkins why couldn't you just nested loop through the contents? If you are only trying to go one way with it, each var in x outputs with y, why couldn't you just have a nested loop? – basic Feb 03 '19 at 06:26

1 Answers1

0

I just tried to figure out an algorithm for this.Its a bit hard to explain. Its just a combinations algorithm.
1.It takes a single item and then combine it with all other remaining.
2.i represents index of input. j represent index of options of current obj. These values don't change until a and b reaches end.

function findComb(arr,result,i,j,a,b){ result.push([arr[i].options[j].text,arr[a].options[b].text]);
 if(arr[a].options[b + 1]) b++;
 else if(arr[a + 1]) a++;
 else{
  if(arr[i].options[j + 1]){
   j++
   a = i + 1
   b = 0
  }
  else if(arr[i + 2]){
   i++;
   j = 0;
   a = i + 1;
            b = 0   
  }
  else return result;
  
 }
 return findComb(arr,result,i,j,a,b)
}


const input1 = [{
    label : 'Colours',
    options: [{text : 'red'}, {text : 'green'}]
},{
    label : 'Sizes',
    options: [{text : 'small'}, {text: 'large'}]
}];
const input2 = [{
    label : 'Colours',
    options: [{text : 'red'}, {text : 'green'},{text:"blue"}]
},{
    label : 'Sizes',
    options: [{text : 'small'}, {text: 'large'},{text: 'medium'}]
}];

console.log(findComb(input1,[],0,0,1,0))
console.log(findComb(input2,[],0,0,1,0))
Maheer Ali
  • 34,163
  • 5
  • 36
  • 62