I'm not sure if this is possible at all, but I would like to do the following. I have the Array of objects, and i want to generate some values from it like this:
const arr = [
{
type: "color",
values: [
{
name: "Color",
option: "Black",
},
{
name: "Color",
option: "Blue",
},
],
},
{
type: "size",
values: [
{
name: "Size",
option: "XS",
},
{
name: "Size",
option: "M",
},
],
},
];
let oldArr: any[] = [];
if (arr.length === 1) {
for (const iterator of arr[arr.length - 1].values) {
oldArr.push(iterator);
}
} else if (arr.length === 2) {
for (const iterator of arr[arr.length - 1].values) {
for (const iterator2 of arr[arr.length - 2].values) {
oldArr.push([iterator, iterator2]);
}
}
} else if (arr.length === 3) {
for (const iterator of arr[arr.length - 1].values) {
for (const iterator2 of arr[arr.length - 2].values) {
for (const iterator3 of arr[arr.length - 3].values) {
oldArr.push([iterator, iterator2, iterator3]);
}
}
}
} else if (arr.length === 4) {
for (const iterator of arr[arr.length - 1].values) {
for (const iterator2 of arr[arr.length - 2].values) {
for (const iterator3 of arr[arr.length - 3].values) {
for (const iterator4 of arr[arr.length - 4].values) {
oldArr.push([iterator, iterator2, iterator3, iterator4]);
}
}
}
}
}
There is possible to do that in recursive mode N times? Added real Array of objects...
Expected Output that i need is an array of array:
const oldArr = [
[
{
"name" : "Color",
"option" : "Blue",
},
{
"name" : "Size",
"option" : "XS"
}
],
[
{
"name" : "Color",
"option" : "Black",
},
{
"name" : "Size",
"option" : "XS"
}
],
[
{
"name" : "Color",
"option" : "Blue",
},
{
"name" : "Size",
"option" : "M"
}
],
[
{
"name" : "Color",
"option" : "Black",
},
{
"name" : "Size",
"option" : "M"
}
]
]
What detail i must to add before posting this?