Search for room selection options

I have the following sample data in 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}
];

Say I already defined a choice using the following variables:

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

When I try to find the number selection options from the above data, then I will have 24 possibilities:

=> 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

Is there anyone who can help me give an algorithm for this, or is there anyone who can help me provide javascript code to create these features?

groupand idmay be unlimited.

+3
source share
3 answers

You compute all permutations to select one element from each group.

, , , 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);
   }
}
+1

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

@mdma :

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]);
}
0

Source: https://habr.com/ru/post/1757101/


All Articles