Javascript - Generating all combinations 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 combinations 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 combinations 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 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;
}
let results = getCombinations(['apple', 'banana', 'lemon', ',mango']);