0

Hello i have a problem and i wanted your expertise on how i can solve it.I want to create products based on different variations.lets say i have

var size = ["sm","xs","large"];

var material = ["cotton","gold"];

var gender: ['Men', 'Women']

My desire output is lets all the different combinations of n arrays in this case

var output = ["sm cotton men","sm gold women" , ...]

My problem is that all the possible solutions that i have found on the internet support 3-4 arrays with 10 variations inside.I wanted to support n arrays with lets say 50 variations each.I have found code sample in stackoverflow but all of them breaks if i used a lot of variations.

somehow i want to put a limit px 50 max and then when the user click the button button again the algorithm run and finished the next 50 until there is no other possible combinations.

so far i have found this code here : Create product variants based on attributes

that works good but its not scalable enough.I want to do the same thing that wordpress does with woocomerce

stelios
  • 49
  • 3
  • 1
    The question you're asking is related to combinations of items. I'd recommend referring to this math guide first to understand the issue: https://www.mathsisfun.com/combinatorics/combinations-permutations.html. – Darrow Hartman Oct 01 '21 at 20:31
  • Also, I'm a little confused as to why the original post didn't help. Is it that you need to run the code in an O(n) time? – Darrow Hartman Oct 01 '21 at 20:32
  • @DarrowHartman yy it is what i need but if i want to generate lets say 5 arrays with 50 differents variations each the code breaks.Stack exceed error.This the problem that i have – stelios Oct 01 '21 at 21:00
  • @stelios 5 arrays of 50 different variations that will give you 5^50 (or 8.81e+34) possible combinations. Of course you will run into a stack overflow since there is no way in hell a browser can hold an array of that size. – Terry Oct 01 '21 at 23:03
  • @Terry thanks for your response..I know that is any way that i can solve this problem lets say with a limit?In the first run it will generate 50 products in the second onother 50 and so on. – stelios Oct 02 '21 at 08:34
  • Even if you want to implement that… a user will never go beyond the first n sets of products. You have so many possible combinations that it defeats the purpose of generating them. At this point this feels like an XY problem to me. – Terry Oct 02 '21 at 10:03
  • do you have a better idea , how to implement that with a different way? – stelios Oct 02 '21 at 12:20

0 Answers0