Possible combinations of an nested array [closed]


This Content is from Stack Overflow. Question asked by user20031007

It’s been 2 days since I have been trying to get the required output. I tried many ways but didn’t solved the purpose.

//Example Input
const categories = [
  {name: 'Size', value: [22, 21, 20]},
  {name: 'Color', value: ['Red', 'Blue']},
  {name: 'Material', value: ['Silk', 'Cotton']}

//Output Required
const possibleCombinations = [
   '22-Red-Silk', '22-Red-Cotton', '22-Blue-Silk',
   '22-Blue-Cotton', '21-Red-Silk', '21-Red-Cotton', 
   '21-Blue-Silk', '21-Blue-Cotton', '20-Red-Silk', 
   '20-Red-Cotton', '20-Blue-Silk', '20-Blue-Cotton'

Length of categories array can vary and the length of value field in each object can also vary.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?