Is it possible to generate values from an Array of objects in the following way?
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]);
}
}
}
}
}
Can this be done recursively N times with real Array of objects given?
The expected output needed is an array of arrays:
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 other details should be included before posting this?