0

Ive got 3 parent arrays that could sometimes be different lengths which are called Options Each Option has values array which can also be different lengths Example of this would be something like this

[
  {
    name: "Option 1",
    values: ["Blue","Red","Orange"]
  },
  {
    name: "Option 2",
    values: ["Small","Medium","Large"]
  },
  {
    name: "Option 3",
    values: ["Cotton","Satin"]
  }
]

The outcome should generate an array like this

[
  {
    title: "Blue/Small/Cotton",
    price: "10.00"
  },
  {
    title: "Blue/Small/Satin",
    price: "10.00"
  },
  {
    title: "Blue/Small/Cotton",
    price: "10.00"
  },
  {
    title: "Blue/Medium/Satin",
    price: "10.00"
  },
]

And So on. Ive tried mapping through the options but knowing the size of the multiple arrays is what I couldnt figure out

SOLUTION I managed to get a solution

product.options.reduce(
      (a, b) => a.flatMap((x) => b.values.map((y) => [...x, y])),
      [[]]
    );
Ian Aleck
  • 53
  • 4
  • 7
  • And the question is? – cottton Oct 24 '21 at 17:36
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Oct 24 '21 at 17:44
  • I managed to Get something here https://stackoverflow.com/questions/8936610/how-can-i-create-every-combination-possible-for-the-contents-of-two-arrays – Ian Aleck Oct 24 '21 at 18:00

0 Answers0