I thought about and searched for it a lot, and couldn't find an answer to this:
I need to write a js-function that gets an object with an unknown number of arrays, which again have an unknown number of elements. Like so:
{
Color: ["Color : Red", "Color : Blue", "Color : Green"],
Size : ["Size : S", "Size : M", "Size : L"],
Material : ["Material : Cotton"],
Something : ["Something : Anything", "Something : Anotherthing"]
}
Again, this could be a whole lot more (or less) arrays and elements, but in this case I would want to achieve an output like this:
{0: "Color : Red > Size : S > Material : Cotton > Something : Anything",
1: "Color : Red > Size : S > Material : Cotton > Something : Anotherthing",
2: "Color : Red > Size : M > Material : Cotton > Something : Anything",
3: "Color : Red > Size : M > Material : Cotton > Something : Anotherthing",
4: "Color : Red > Size : L > Material : Cotton > Something : Anything",
5: "Color : Red > Size : L > Material : Cotton > Something : Anotherthing",
6: "Color : Blue > Size : S > Material : Cotton > Something : Anything",
...
...[and so forth... ]}
I tried to do a loop in a loop in a loop, but that failed. I then tried first finding the longest array, extracting it from the rest and then looping through every arrays for every element in the longest:
createMap = function(tagObj, longest){
var longObj = tagObj[longest];
var current = {};
delete tagObj[longest];
$.each(tagObj, function(name, obj){
$.each(obj, function(index, tag){
$.each(longObj, function(i, iniTag){
if (current[i]!= undefined){
current[i] += " > " + tag;
}
else current[i] = iniTag + " > " + tag;
})
})
})
console.log(current);
}
But that just results in:
{0: "Color : Red, Size : S, Size : M, Size : L, .... "}
I hope I'm no just overlooking something really obvious - but I spent far too much time on it and just couldn't figure it out. And now I'm a nervous wreck and not able to think straight anymore. I would very much appreciate some help! Thanks in advance!