0

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?

6
  • Sounds like a good candidate for recursion. Commented Sep 7, 2022 at 21:01
  • Yes, Working like a charm! I want to generate variation from diferent type of product! Like woocomerce variations! Just edited a post, i forgot to rename result! Commented Sep 7, 2022 at 21:04
  • TLDR: This kind of branching/looping/nesting is a code smell in need of refactoring. Commented Sep 7, 2022 at 21:08
  • It's possible and looks like it is called a cartesian product of arrays. Can you please give an example for the input and expected output? Commented Sep 7, 2022 at 21:19
  • Added real Array of objects... Commented Sep 7, 2022 at 21:49

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thx, this version is working at 99%! When arr length is === 1, must return a single array inside another array, not array of single object! But this is not a problem, now, i can edit this function! Thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.