admin管理员组

文章数量:1023803

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

So I've seen this questions and I've seen some of the answers to it, but I was wondering if it was possible to get all binations even if it's a duplicate in every order possible.

ex.

var array = ["apple", "banana", "lemon", "mango"];

output

var result = [
   "apple"
   "apple banana"
   "apple banana lemon"
   "apple banana lemon mango"
   "apple lemon"
   "apple lemon banana mango"
   "apple lemon mango banana"
   "apple mango"
   "apple mango lemon banana"
   "apple mango banana lemon"
   "banana"
   "banana apple"
   "banana apple lemon"
   "banana apple mango"
   "banana lemon"
   "banana lemon apple"
   "banana lemon apple mango"
   "banana mango"
   ...
];

In the end I essentially want all possible binations whether it be 1 item or pairs or multiple items in every possible order.

The closest answer I've seen is this set of code.

function getCombinations(valuesArray: String[])
{

var bi = [];
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)
    {
        bi.push(temp);
    }
}

bi.sort((a, b) => a.length - b.length);
console.log(bi.join("\n"));
return bi;
}
let results = getCombinations(['apple', 'banana', 'lemon', ',mango']);

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

So I've seen this questions and I've seen some of the answers to it, but I was wondering if it was possible to get all binations even if it's a duplicate in every order possible.

ex.

var array = ["apple", "banana", "lemon", "mango"];

output

var result = [
   "apple"
   "apple banana"
   "apple banana lemon"
   "apple banana lemon mango"
   "apple lemon"
   "apple lemon banana mango"
   "apple lemon mango banana"
   "apple mango"
   "apple mango lemon banana"
   "apple mango banana lemon"
   "banana"
   "banana apple"
   "banana apple lemon"
   "banana apple mango"
   "banana lemon"
   "banana lemon apple"
   "banana lemon apple mango"
   "banana mango"
   ...
];

In the end I essentially want all possible binations whether it be 1 item or pairs or multiple items in every possible order.

The closest answer I've seen is this set of code.

function getCombinations(valuesArray: String[])
{

var bi = [];
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)
    {
        bi.push(temp);
    }
}

bi.sort((a, b) => a.length - b.length);
console.log(bi.join("\n"));
return bi;
}
let results = getCombinations(['apple', 'banana', 'lemon', ',mango']);

Share Improve this question edited Apr 14, 2022 at 22:02 Nikster asked Apr 14, 2022 at 21:55 NiksterNikster 4841 gold badge6 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

At first create function to find all binations of array:

function binations(array) {
  return new Array(1 << array.length).fill().map(
    (e1, i) => array.filter((e2, j) => i & 1 << j));
}

Then you have to create function to generate all permutations of given array (for example this using Heap's method:

function permute(permutation) {
  var length = permutation.length,
      result = [permutation.slice()],
      c = new Array(length).fill(0),
      i = 1, k, p;

  while (i < length) {
    if (c[i] < i) {
      k = i % 2 && c[i];
      p = permutation[i];
      permutation[i] = permutation[k];
      permutation[k] = p;
      ++c[i];
      i = 1;
      result.push(permutation.slice());
    } else {
      c[i] = 0;
      ++i;
    }
  }
  return result;
}

After that you can bine that to final function:

function calculateAllCombinations(myArray) {
    var allValues = binations(myArray)

    var response = allValues

    for(let v of allValues) {
        response = response.concat(permute(v))
    }

    //Return removed duplicates
    return Array.from(new Set(response.map(JSON.stringify)), JSON.parse)

}

At the end you can call it like this:

var myArray = ["apple", "banana", "lemon", "mango"]

var allValues = calculateAllCombinations(myArray)

function binations(array) {
  return new Array(1 << array.length).fill().map(
    (e1, i) => array.filter((e2, j) => i & 1 << j));
}

function permute(permutation) {
  var length = permutation.length,
      result = [permutation.slice()],
      c = new Array(length).fill(0),
      i = 1, k, p;

  while (i < length) {
    if (c[i] < i) {
      k = i % 2 && c[i];
      p = permutation[i];
      permutation[i] = permutation[k];
      permutation[k] = p;
      ++c[i];
      i = 1;
      result.push(permutation.slice());
    } else {
      c[i] = 0;
      ++i;
    }
  }
  return result;
}
function calculateAllCombinations(myArray) {
    var allValues = binations(myArray)

    var response = allValues

    for(let v of allValues) {
        response = response.concat(permute(v))
    }

    //Return removed duplicates
    return Array.from(new Set(response.map(JSON.stringify)), JSON.parse)

}
var myArray = ["apple", "banana", "lemon", "mango"]

var allValues = calculateAllCombinations(myArray)

console.log(allValues);
.as-console-wrapper{top:0;max-height:unset!important;overflow:auto!important;}

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

So I've seen this questions and I've seen some of the answers to it, but I was wondering if it was possible to get all binations even if it's a duplicate in every order possible.

ex.

var array = ["apple", "banana", "lemon", "mango"];

output

var result = [
   "apple"
   "apple banana"
   "apple banana lemon"
   "apple banana lemon mango"
   "apple lemon"
   "apple lemon banana mango"
   "apple lemon mango banana"
   "apple mango"
   "apple mango lemon banana"
   "apple mango banana lemon"
   "banana"
   "banana apple"
   "banana apple lemon"
   "banana apple mango"
   "banana lemon"
   "banana lemon apple"
   "banana lemon apple mango"
   "banana mango"
   ...
];

In the end I essentially want all possible binations whether it be 1 item or pairs or multiple items in every possible order.

The closest answer I've seen is this set of code.

function getCombinations(valuesArray: String[])
{

var bi = [];
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)
    {
        bi.push(temp);
    }
}

bi.sort((a, b) => a.length - b.length);
console.log(bi.join("\n"));
return bi;
}
let results = getCombinations(['apple', 'banana', 'lemon', ',mango']);

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

So I've seen this questions and I've seen some of the answers to it, but I was wondering if it was possible to get all binations even if it's a duplicate in every order possible.

ex.

var array = ["apple", "banana", "lemon", "mango"];

output

var result = [
   "apple"
   "apple banana"
   "apple banana lemon"
   "apple banana lemon mango"
   "apple lemon"
   "apple lemon banana mango"
   "apple lemon mango banana"
   "apple mango"
   "apple mango lemon banana"
   "apple mango banana lemon"
   "banana"
   "banana apple"
   "banana apple lemon"
   "banana apple mango"
   "banana lemon"
   "banana lemon apple"
   "banana lemon apple mango"
   "banana mango"
   ...
];

In the end I essentially want all possible binations whether it be 1 item or pairs or multiple items in every possible order.

The closest answer I've seen is this set of code.

function getCombinations(valuesArray: String[])
{

var bi = [];
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)
    {
        bi.push(temp);
    }
}

bi.sort((a, b) => a.length - b.length);
console.log(bi.join("\n"));
return bi;
}
let results = getCombinations(['apple', 'banana', 'lemon', ',mango']);

Share Improve this question edited Apr 14, 2022 at 22:02 Nikster asked Apr 14, 2022 at 21:55 NiksterNikster 4841 gold badge6 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

At first create function to find all binations of array:

function binations(array) {
  return new Array(1 << array.length).fill().map(
    (e1, i) => array.filter((e2, j) => i & 1 << j));
}

Then you have to create function to generate all permutations of given array (for example this using Heap's method:

function permute(permutation) {
  var length = permutation.length,
      result = [permutation.slice()],
      c = new Array(length).fill(0),
      i = 1, k, p;

  while (i < length) {
    if (c[i] < i) {
      k = i % 2 && c[i];
      p = permutation[i];
      permutation[i] = permutation[k];
      permutation[k] = p;
      ++c[i];
      i = 1;
      result.push(permutation.slice());
    } else {
      c[i] = 0;
      ++i;
    }
  }
  return result;
}

After that you can bine that to final function:

function calculateAllCombinations(myArray) {
    var allValues = binations(myArray)

    var response = allValues

    for(let v of allValues) {
        response = response.concat(permute(v))
    }

    //Return removed duplicates
    return Array.from(new Set(response.map(JSON.stringify)), JSON.parse)

}

At the end you can call it like this:

var myArray = ["apple", "banana", "lemon", "mango"]

var allValues = calculateAllCombinations(myArray)

function binations(array) {
  return new Array(1 << array.length).fill().map(
    (e1, i) => array.filter((e2, j) => i & 1 << j));
}

function permute(permutation) {
  var length = permutation.length,
      result = [permutation.slice()],
      c = new Array(length).fill(0),
      i = 1, k, p;

  while (i < length) {
    if (c[i] < i) {
      k = i % 2 && c[i];
      p = permutation[i];
      permutation[i] = permutation[k];
      permutation[k] = p;
      ++c[i];
      i = 1;
      result.push(permutation.slice());
    } else {
      c[i] = 0;
      ++i;
    }
  }
  return result;
}
function calculateAllCombinations(myArray) {
    var allValues = binations(myArray)

    var response = allValues

    for(let v of allValues) {
        response = response.concat(permute(v))
    }

    //Return removed duplicates
    return Array.from(new Set(response.map(JSON.stringify)), JSON.parse)

}
var myArray = ["apple", "banana", "lemon", "mango"]

var allValues = calculateAllCombinations(myArray)

console.log(allValues);
.as-console-wrapper{top:0;max-height:unset!important;overflow:auto!important;}

本文标签: JavascriptAll Possible Combinations From Single Array Every OrderStack Overflow