Javascript - Generating all combinations of elements in a single array (in pairs)

后端 未结 9 616
天命终不由人
天命终不由人 2020-11-28 11:14

I\'ve seen several similar questions about how to generate all possible combinations of elements in an array. But I\'m having a very hard time figuring out how to write an a

相关标签:
9条回答
  • 2020-11-28 11:33

    In my case, I wanted to get the combinations as follows, based on the size range of the array:

    function getCombinations(valuesArray: String[])
    {
    
    var combi = [];
    var temp = [];
    var slent = Math.pow(2, valuesArray.length);
    
    for (var i = 0; i < slent; i++)
    {
        temp = [];
        for (var j = 0; j < valuesArray.length; j++)
        {
            if ((i & Math.pow(2, j)))
            {
                temp.push(valuesArray[j]);
            }
        }
        if (temp.length > 0)
        {
            combi.push(temp);
        }
    }
    
    combi.sort((a, b) => a.length - b.length);
    console.log(combi.join("\n"));
    return combi;
    }
    

    Example:

    // variable "results" stores an array with arrays string type
    let results = getCombinations(['apple', 'banana', 'lemon', ',mango']);
    

    Output in console:

    The function is based on the logic of the following documentation, more information in the following reference: https://www.w3resource.com/javascript-exercises/javascript-function-exercise-3.php

    0 讨论(0)
  • 2020-11-28 11:39

    A simple way would be to do a double for loop over the array where you skip the first i elements in the second loop.

    let array = ["apple", "banana", "lemon", "mango"];
    let results = [];
    
    // Since you only want pairs, there's no reason
    // to iterate over the last element directly
    for (let i = 0; i < array.length - 1; i++) {
      // This is where you'll capture that last value
      for (let j = i + 1; j < array.length; j++) {
        results.push(`${array[i]} ${array[j]}`);
      }
    }
    
    console.log(results);

    Rewritten with ES5:

    var array = ["apple", "banana", "lemon", "mango"];
    var results = [];
    
    // Since you only want pairs, there's no reason
    // to iterate over the last element directly
    for (var i = 0; i < array.length - 1; i++) {
      // This is where you'll capture that last value
      for (var j = i + 1; j < array.length; j++) {
        results.push(array[i] + ' ' + array[j]);
      }
    }
    
    console.log(results);

    0 讨论(0)
  • 2020-11-28 11:40

    Here are some functional programming solutions:

    Using EcmaScript2019's flatMap:

    var array = ["apple", "banana", "lemon", "mango"];
    
    var result = array.flatMap(
        (v, i) => array.slice(i+1).map( w => v + ' ' + w )
    );
    
    console.log(result);

    Before the introduction of flatMap (my answer in 2017), you would go for reduce or [].concat(...) in order to flatten the array:

    var array = ["apple", "banana", "lemon", "mango"];
    
    var result = array.reduce( (acc, v, i) =>
        acc.concat(array.slice(i+1).map( w => v + ' ' + w )),
    []);
    
    console.log(result);

    Or:

    var array = ["apple", "banana", "lemon", "mango"];
    
    var result = [].concat(...array.map( 
        (v, i) => array.slice(i+1).map( w => v + ' ' + w ))
    );
    
    console.log(result);

    0 讨论(0)
  • 2020-11-28 11:42

    Try this: https://jsfiddle.net/e2dLa9v6/

    var array = ["apple", "banana", "lemon", "mango"];
    var result = [];
    
    for(var i=0;i<array.length-1;i++){
        for(var j=i+1;j<array.length;j++){
        result.push(array[i]+" "+array[j]);
      }
    }
    for(var i=0;i<result.length;i++){
        alert(result[i]);
    }
    
    0 讨论(0)
  • 2020-11-28 11:48

    Using map and flatMap the following can be done (flatMap is only supported on chrome and firefox)

    var array = ["apple", "banana", "lemon", "mango"]
    array.flatMap(x => array.map(y => x !== y ? x + ' ' + y : null)).filter(x => x)
    
    0 讨论(0)
  • 2020-11-28 11:50

    Although solutions have been found, I post here an algorithm for general case to find all combinations size n of m (m>n) elements. In your case, we have n=2 and m=4.

    const result = [];
    result.length = 2; //n=2
    
    function combine(input, len, start) {
      if(len === 0) {
        console.log( result.join(" ") ); //process here the result
        return;
      }
      for (let i = start; i <= input.length - len; i++) {
        result[result.length - len] = input[i];
        combine(input, len-1, i+1 );
      }
    }
    
    const array = ["apple", "banana", "lemon", "mango"];    
    combine( array, result.length, 0);

    0 讨论(0)
提交回复
热议问题