jquery数组特定组合的串联,除非重复

这种方式的数据:

[Array1] = ['blue','green', 'yellow','red','very very red'] [Array2] = ['ColumnA','ColumnB','ColumnC','ColumnD','ColumnD'] 

这导致两行。 所需的JSON输出:

 { 'row1': {'ColumnA':'blue','ColumnB':'green','ColumnC':'yellow','ColumnD':'red'} 'row2': {'ColumnA':'blue','ColumnB':'green',,'ColumnC':'yellow','ColumnD':'very very red'} } 

请注意,Array1和Array2是成对的。 ColumnD有两个案例。

问题是Array2可以有任意数量的重复项(例如ColumnA的另一个案例)。

循环的数量和建立索引表以跟踪循环的前景是一个令人生畏的前景。

我正在寻找有关如何做到这一点的建议。 我知道sql是一个更好的编程选择,但我正在研究一个Jquery函数。

编辑:工作解决方案;)

问题是,我实际上冲向了错误的方向。 我提出了以下想法,它使用递归函数,因此使其更容易阅读。

实际上,您希望拥有所有组合,而不需要排列。 这里我只放了一个颜色的第一个字母。 所以,来自:

 b1 b2 g1 y1 y2 r1 r2 r3 AABCCDDD 

你想要(这里的顺序是代码将做的):

 ABCD b1 g1 y1 r1 b1 g1 y1 r2 b1 g1 y1 r3 b1 g1 y2 r1 b1 g1 y2 r2 b1 g1 y2 r3 b2 g1 y1 r1 b2 g1 y1 r2 b2 g1 y1 r3 b2 g1 y2 r1 b2 g1 y2 r2 b2 g1 y2 r3 

首先,我想要列/颜色数组的另一种格式,因为它更容易使用。 我想离开

 [b1,b2,g1,y1,y2,r1,r2,r3] [A ,A ,B ,C ,C ,D ,D ,D] 

 [[b1,b2],[g1],[y1,y2],[r1,r2,r3]] [ A , B , C , D ] 

这样,对于每个列值,我们将具有包含与列相关的所有颜色的匹配子arrays。

以下function实现了此目的(即使它可能不是最佳方式):

 //column and color are the paired arrays you have as data input function arrangeArrays(column, color) { var ret=new Array(); //the returned container for the data ret["color"]=new Array(); //the color part of the returned data ret["column"]=new Array(); //the column part of the returned data var tmp=new Array(); //an internal array we'll use to switch from associative keys to normal keys //we parse the paired arrays for(var i in column) { //if the column is not an associative key in tmp, we declare it as an array if(!tmp[column[i]]) tmp[column[i]]=new Array(); //we add the color to the subarray matching its column tmp[column[i]].push(color[i]); } //now we just translate these horrible associative keys into cute array-standard integer keys for(var i in tmp) { ret["color"].push(tmp[i]); ret["column"].push(i); } //the commented code is a quick does-it-work block /* for(var i in ret["column"]) { document.write("column="+ret["column"][i]+" --- color(s)="+ret["color"][i].join()+"
"); } */ return ret; }

现在到了核心。 对函数的每次调用都将处理一列,实际上只有一部分,使用递归来处理其他列。 使用简化示例,代码执行此操作:

 [[b1,b2],[y1,y2]] [ A , B ] total combinations: 2*2=4 first call for 1st column, length is 4, 2 possible values, first insert loops 4/2=2 times: b1 b1 call for 2nd column, length is 2, 2 possible values, first insert loops 2/2=1 time: b1 y1 b1 call for 3rd column, no 3rd column, coming back call for 2nd column, length is 2, 2 possible values, second insert loops 2/2=1 time: b1 y1 b1 y2 call for 3rd column, no 3rd column, coming back call for 2nd column done, coming back first call for 1st column, length is 4, 2 possible values, second insert loops 4/2=2 times: b1 y1 b1 y2 b2 b2 call for 2nd column, length is 2, 2 possible values, first insert loops 2/2=1 time: b1 y1 b1 y2 b2 y1 b2 call for 3rd column, no 3rd column, coming back call for 2nd column, length is 2, 2 possible values, second insert loops 2/2=1 time: b1 y1 b1 y2 b2 y1 b2 y2 call for 3rd column, no 3rd column, coming back call for 2nd column done, coming back call for 1st column done, coming back (returning) 

这是代码,帮助自己阅读评论;)

 //results is an empty array, it is used internally to pass the processed data through recursive calls //column and color would be the subarrays indexed by "column" and "color" from the data received by arrangeArrays() //resultIndex is zero, it is used for recursive calls, to know where we start inserting data in results //length is the total of results expected; in our example, we have 2 blues, 1 green, 2 yellows, and 3 reds: the total number of combinations will be 2*1*2*3, it's 12 //resourceIndex is zero, used for recursive calls, to know what column we are to insert in results function go(results, column, color, resultIndex, length, resourceIndex) { //this case stops the recursion, it means the current call tries to exceed the length of the resource arrays; so we just return the data without touching it if(resourceIndex>=column.length) return results; //we loop on every color mentioned in a column for(var i=0;i 

我使用这段代码测试了这个函数(如果你想体验每一步发生的事情,可能会很有用):

 function parseResults(res) { for(x in res) { //x is an index of the array (integer) for(var y in res[x]) { //y is a column name document.write(x+" : "+y+" = "+res[x][y]); //res[x][y] is a color document.write("
"); } document.write("
"); } }

您可能还需要一个函数来告诉go()第一次调用使用的长度。 像这个:

 function getLength(color) { var r=1; for(var i in color) r*=color[i].length; return r; } 

现在,假设您有一个数组column和一个数组color

 var arrangedArrays=arrangeArrays(column, color); var res=go(new Array(), arrangedArrays["column"], arrangedArrays["color"], 0, getLength(arrangedArrays["color"]), 0); 

在这里,似乎很大,但我想解释得很好,无论如何,如果你删除代码和示例中的注释,它不是那么大......整个事情就是不要对这些索引发疯;)

以下是第一个答案 ,但效果不好......但是,这些都没有用。

您可以使用“关联数组”(或eval类似对象/属性的语法,它大致相同)。 像这样的东西:

 var arrayResult=new Array(); var resultLength=0; for(var globalCounter=0;globalCounter 

从那里,我想很容易将其翻译成JSON格式。

解析子数组时,请记住关联键实际上是方法 ,即你不能用for(x=0;x循环,而是for(x in array) ; 并且你必须测试密钥并确保它在处理之前以“column”开头(或者你将结束处理对“length”/ 0 ^^)。 如果您希望此容器数组具有这种键,则在arrayResult的键上相同。

最后一件事:我没有测试代码,可能会错过一些,或者可能会有一些错误。 目的是帮助,而不是做:)

祝好运!