Home > Back-end >  Nested for of loop inside multiple for loops typescript
Nested for of loop inside multiple for loops typescript

Time:09-08

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?

CodePudding user response:

Here is an example of a recursive function that should achieve your results. I have tested it for inputs up to 4 nested loops and it seems to work. The arguments are mostly self explanatory, arr is the array you are reading, oldArr is the array you are writing to, vals is a list used for keeping track of all the iterator values (you should pass an empty list to start!), level is the "level of recursion" and target_level is the final level of recursion you want. An example of how to run this might be recursive_func(arr, oldArr, [], 1, 4) if you want to achieve the same result as your 4 nested loop example.

If the number of nested for loops is always the same as the length of arr please let me know so that the function can be slightly simplified.

function recursive_func(arr, oldArr, vals, level, target_level) {
    for (const it_n of arr[arr.length - level].values) {
        vals.push(it_n);
        if (level == target_level) {
            oldArr.push(JSON.parse(JSON.stringify(vals)));
        } else {
            recursive_func(arr, oldArr, vals, level   1, target_level);
        }
         vals.pop()
    }
}

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 = [];
recursive_func(arr, oldArr, [], 1, arr.length);

console.log(oldArr);

  • Related