我有四个像这样的数组
var Broker = ['A', 'B', 'C'];
var Currency = ['C', 'D', 'E'];
var Time = ['F', 'G', 'H', 'I'];
var Mode = ['J', 'K', 'L'];
因此所有这些数组都显示在多个select separtely中。当用户从每个多选下拉列表中选择多个字段时,我想列出所选项目的所有配对可能性
例如if user choose A and B from Broker, C from currency, F,G from time and J from mode the paring possiblity should be stored in another separate array like this
var paired = [{borker:A,currency:C,time:F, mode: J},{borker:A,currency:C,time:G, mode: J},{borker:A,currency:C,time:F, mode: J}, {borker:B,currency:C,time:F, mode: J},{borker:B,currency:C,time:G, mode: K},{borker:B,currency:C,time:F, mode: L}];
我可能错过了paried数组中的项目但是我需要来自所有选定数组的至少一个唯一项目。它有点类型。所以你们可以得到这样的结果。
答案 0 :(得分:2)
此代码创建一个名为permutations
的数组,该数组包含每个可能的选择排列的对象。嵌套的for循环是排列的技巧。
var Broker = ['A', 'B', 'C'];
var Currency = ['C', 'D', 'E'];
var Time = ['F', 'G', 'H', 'I'];
var Mode = ['J', 'K', 'L'];
var permutations = [];
for(var i = 0; i < Broker.length; i++) {
for(var j = 0; j < Currency.length; j++) {
for(var k = 0; k < Time.length; k++) {
for(var l = 0; l < Mode.length; l++) {
permutations.push({
borker:Broker[i],
currency:Currency[j],
time:Time[k],
mode:Mode[l]
});
}
}
}
}