2010-07-29 84 views
3

我在JavaScript中有下面的示例數據:查找號碼選擇的可能性

var variations = [ 
    {group: 1, id: 1}, 
    {group: 1, id: 2}, 
    {group: 1, id: 3}, 
    {group: 1, id: 4}, 
    {group: 2, id: 5}, 
    {group: 2, id: 6}, 
    {group: 2, id: 7}, 
    {group: 3, id: 8}, 
    {group: 3, id: 9} 
]; 

讓我們說,我已經使用下列變量定義選擇:

var selected_variation_groups = [1,2,3]; 
var selected_variation_group_ids = [1,2,3,4,5,6,7,8,9]; 

當我試圖找到選擇然後我有24個可能性:

=> 1, 5, 8 
=> 1, 5, 9 
=> 1, 6, 8 
=> 1, 6, 9 
=> 1, 7, 8 
=> 1, 7, 9 

=> 2, 5, 8 
=> 2, 5, 9 
=> 2, 6, 8 
=> 2, 6, 9 
=> 2, 7, 8 
=> 2, 7, 9 

=> 3, 5, 8 
=> 3, 5, 9 
=> 3, 6, 8 
=> 3, 6, 9 
=> 3, 7, 8 
=> 3, 7, 9 

=> 4, 5, 8 
=> 4, 5, 9 
=> 4, 6, 8 
=> 4, 6, 9 
=> 4, 7, 8 
=> 4, 7, 9 

是任何可以幫助我爲此提供算法的人,還是有人可以幫助我提供JavaScript代碼來創建這些可能性?

group s和id s可以是無限的。

回答

1

您正在計算從每個組中選擇一個項目的所有排列。

如果將所有組排列爲一個數組,則所有組的數據都更容易編碼,因此所有組1的數據都在一個數組中,組2在另一個數組中,等等。

此函數將組中的所有排列添加到一個數組,並用逗號分隔值。

var groups = [ 
     [1,2,3,4], [5,6,7], [8,9] 
]; 

var result = new Array(); 
appendPermutation(groups, 0, groups.length, "", result); 

alert(result.length); 
alert(result); 


function appendPermutation(groups, start, end, currentResult, result) 
{ 
    if (start==end) 
    { 

     result.push(currentResult); 
     return; 
    } 

    var group = groups[start]; 
    for (var i=0; i<group.length; i++) { 
     var value = group[i].toString(); 
     var nextResult; 
     if (currentResult.length==0) 
      nextResult = currentResult + value; 
     else 
      nextResult = currentResult + "," + value;  
     appendPermutation(groups, start+1, end, nextResult, result); 
    } 
} 
+0

我發現了一個網上JS評估,所以固定幾個拼寫錯誤,這是現在的工作。 – mdma 2010-07-29 08:58:42

+0

+1前一陣子。我從組織數據開始,並且已經發布了一個解決方案。我已經發布了完整性的第一步。 – Kobi 2010-07-29 09:03:41

+0

謝謝@mdma的解決方案。 – sule 2010-07-29 09:40:11

0

mdma已經展示瞭如何獲得排列。此代碼將組數據的哈希集合,如推薦:

var groups = {}; 
for(var i = 0; i < variations.length; i++){ 
    var key = variations[i].group; 
    var value = variations[i].id; 

    if(!groups[key]) 
     groups[key] = []; 
    groups[key].push(value); 
} 

如果你需要一個數組變化groups = {};groups = [];,但要確保鍵是小數目,也可能產生大量數組(大數字),或添加屬性(用於其他字符串)。

0

除了@搖頭丸的解決方案,你會發現這個也很有用:

function permutation(options) { 
    if(options.length == 1) { 
     var permutations = []; 
     for(var i = 0; i < options[0].length; i++) { 
      permutations.push([options[0][i]]); 
     } 
     return permutations; 
    } 
    return addOptionWithPermutation(options[0], permutation(options.slice(1))); 
} 

function addOptionWithPermutation(option, permutations) { 
    var newPermutations = []; 
    for(var i = 0; i < option.length; i++) { 
     for(var j = 0; j < permutations.length; j++) { 
      var newPerm = permutations[j].slice(0); //just to take copy 
      newPerm.splice(0, 0, option[i]); //insert in the beginning 
      newPermutations.push(newPerm); 
     } 
    } 
    return newPermutations; 
} 

var permutations = permutation([[1,2,3,4], [5,6,7], [8,9]]); 

for(var i = 0; i < permutations.length; i++) { 
    alert(permutations[i]); 
}