Data in this fashion:
[Array1] = ['blue','green', 'yellow','red','very very red']
[Array2] = ['ColumnA','ColumnB','ColumnC','ColumnD','ColumnD']
This results in two rows. Desired JSON output:
{ 'row1': {'ColumnA':'blue','ColumnB':'green','ColumnC':'yellow','ColumnD':'red'}
'row2': {'ColumnA':'blue','ColumnB':'green',,'ColumnC':'yellow','ColumnD':'very very red'}
}
Notice that Array1 and Array2 are pairs. ColumnD has two cases.
The catch is that Array2 can have any number of duplicates (for example another case with ColumnA).
The number of loops and prospect of building index tables to keep track of the loops is a daunting prospect.
I’m looking for advice on how to do this. I know sql would be a better programming choice, but I’m investigating a Jquery function.
Edit: the working solution 😉
The thing is, I actually rushed into the wrong direction. I came up with the following idea, which uses a recursive function therefore making it much more easy to read.
Indeed, you want to have all the combinations, without the permutations. Here I put only the first letter of a color. So, from:
You want to have (the order here is what the code will do):
First, I wanted another format for the column/color array, as it would be easier to work with. I wanted to go from
to
This way we would have, for each column value, a matching subarray containg all the colors related to the column.
The following function achieves this purpose (even though it is probably not the optimal way to do it):
Now to the core. Each call to the function will process one column, and actually only a part of it, using recursion to deal with other columns. Using a simplified example, the code does this:
Here is the code, help yourself reading the comments 😉
I tested the function using this bit of code (might be useful if you want to experience what happens on each step):
You’ll probably also need a function to tell
go()what length to use for the first call. Like this one:Now, assuming you have an array
columnand an arraycolor:Here it is, seems big but I wanted to explain well, and anyway if you remove the comments in the code and the examples, it’s not that big… The whole thing is just about not going crazy with these indexes 😉
This below was the first answer, which didn’t work well… which, well, didn’t work.
You can use “associative arrays” (or
evalan object/property-like syntax, it’s about the same). Something like that:From there, I suppose it’s easy to translate it to JSON format.
When you parse the subarrays, keep in mind that associative keys are actually methods, i.e. you can’t loop with
for(x=0;x<array.length;x++), instead usefor(x in array); and you have to test the key and be sure it starts with “column” before processing it (or you’ll end processing the pair “length”/0 ^^). Same on arrayResult’s keys if you want this container array to have that kind of keys.One last thing: I didn’t test the code, it may miss a bit, or there may be some mistyping. The purpose is to help, not to do 🙂
Good luck!