Combination without repetition javascript [duplicate]
This question already has an answer here:
JavaScript - Generating combinations from n arrays with m elements
8 answers
Given
[
["blue", "red"],
[1, 2],
[true, false],
]
How can I get the possible combinations in javascript?:
blue, 1, true
blue, 1, false
blue, 2, true
blue, 2, false
red, 1, true
red, 1, false
red, 2, true
red, 2, false
Order doesn't matter.
javascript combinations permutation
marked as duplicate by Always Sunny, Graham, Foo, Hassan Imam, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 7:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
JavaScript - Generating combinations from n arrays with m elements
8 answers
Given
[
["blue", "red"],
[1, 2],
[true, false],
]
How can I get the possible combinations in javascript?:
blue, 1, true
blue, 1, false
blue, 2, true
blue, 2, false
red, 1, true
red, 1, false
red, 2, true
red, 2, false
Order doesn't matter.
javascript combinations permutation
marked as duplicate by Always Sunny, Graham, Foo, Hassan Imam, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 7:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
JavaScript - Generating combinations from n arrays with m elements
8 answers
Given
[
["blue", "red"],
[1, 2],
[true, false],
]
How can I get the possible combinations in javascript?:
blue, 1, true
blue, 1, false
blue, 2, true
blue, 2, false
red, 1, true
red, 1, false
red, 2, true
red, 2, false
Order doesn't matter.
javascript combinations permutation
This question already has an answer here:
JavaScript - Generating combinations from n arrays with m elements
8 answers
Given
[
["blue", "red"],
[1, 2],
[true, false],
]
How can I get the possible combinations in javascript?:
blue, 1, true
blue, 1, false
blue, 2, true
blue, 2, false
red, 1, true
red, 1, false
red, 2, true
red, 2, false
Order doesn't matter.
This question already has an answer here:
JavaScript - Generating combinations from n arrays with m elements
8 answers
javascript combinations permutation
javascript combinations permutation
asked Nov 15 '18 at 6:03
Mauro AguilarMauro Aguilar
216213
216213
marked as duplicate by Always Sunny, Graham, Foo, Hassan Imam, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 7:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Always Sunny, Graham, Foo, Hassan Imam, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 7:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
this was quite complex, and lots of fun, so thanks for asking!
as here we can have m element arrays of arbitary size,
var a = [
["blue", "red"],
[1, 2],
[true, false],
]
function allPossibleCombinations(items, isCombination=false){
// finding all possible combinations of the last 2 items
// remove those 2, add these combinations
// isCombination shows if the last element is itself part of the combination series
if(items.length == 1){
return items[0]
}
else if(items.length == 2){
var combinations =
for (var i=0; i<items[1].length; i++){
for(var j=0; j<items[0].length; j++){
if(isCombination){
// clone array to not modify original array
var combination = items[1][i].slice();
combination.push(items[0][j]);
}
else{
var combination = [items[1][i], items[0][j]];
}
combinations.push(combination);
}
}
return combinations
}
else if(items.length > 2){
var last2 = items.slice(-2);
var butLast2 = items.slice(0, items.length - 2);
last2 = allPossibleCombinations(last2, isCombination);
butLast2.push(last2)
var combinations = butLast2;
return allPossibleCombinations(combinations, isCombination=true)
}
}
console.log(allPossibleCombinations(a));
console.log(allPossibleCombinations(a).length);
pretty impressive, works just fine, thank you.
– Mauro Aguilar
Nov 15 '18 at 9:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
this was quite complex, and lots of fun, so thanks for asking!
as here we can have m element arrays of arbitary size,
var a = [
["blue", "red"],
[1, 2],
[true, false],
]
function allPossibleCombinations(items, isCombination=false){
// finding all possible combinations of the last 2 items
// remove those 2, add these combinations
// isCombination shows if the last element is itself part of the combination series
if(items.length == 1){
return items[0]
}
else if(items.length == 2){
var combinations =
for (var i=0; i<items[1].length; i++){
for(var j=0; j<items[0].length; j++){
if(isCombination){
// clone array to not modify original array
var combination = items[1][i].slice();
combination.push(items[0][j]);
}
else{
var combination = [items[1][i], items[0][j]];
}
combinations.push(combination);
}
}
return combinations
}
else if(items.length > 2){
var last2 = items.slice(-2);
var butLast2 = items.slice(0, items.length - 2);
last2 = allPossibleCombinations(last2, isCombination);
butLast2.push(last2)
var combinations = butLast2;
return allPossibleCombinations(combinations, isCombination=true)
}
}
console.log(allPossibleCombinations(a));
console.log(allPossibleCombinations(a).length);
pretty impressive, works just fine, thank you.
– Mauro Aguilar
Nov 15 '18 at 9:57
add a comment |
this was quite complex, and lots of fun, so thanks for asking!
as here we can have m element arrays of arbitary size,
var a = [
["blue", "red"],
[1, 2],
[true, false],
]
function allPossibleCombinations(items, isCombination=false){
// finding all possible combinations of the last 2 items
// remove those 2, add these combinations
// isCombination shows if the last element is itself part of the combination series
if(items.length == 1){
return items[0]
}
else if(items.length == 2){
var combinations =
for (var i=0; i<items[1].length; i++){
for(var j=0; j<items[0].length; j++){
if(isCombination){
// clone array to not modify original array
var combination = items[1][i].slice();
combination.push(items[0][j]);
}
else{
var combination = [items[1][i], items[0][j]];
}
combinations.push(combination);
}
}
return combinations
}
else if(items.length > 2){
var last2 = items.slice(-2);
var butLast2 = items.slice(0, items.length - 2);
last2 = allPossibleCombinations(last2, isCombination);
butLast2.push(last2)
var combinations = butLast2;
return allPossibleCombinations(combinations, isCombination=true)
}
}
console.log(allPossibleCombinations(a));
console.log(allPossibleCombinations(a).length);
pretty impressive, works just fine, thank you.
– Mauro Aguilar
Nov 15 '18 at 9:57
add a comment |
this was quite complex, and lots of fun, so thanks for asking!
as here we can have m element arrays of arbitary size,
var a = [
["blue", "red"],
[1, 2],
[true, false],
]
function allPossibleCombinations(items, isCombination=false){
// finding all possible combinations of the last 2 items
// remove those 2, add these combinations
// isCombination shows if the last element is itself part of the combination series
if(items.length == 1){
return items[0]
}
else if(items.length == 2){
var combinations =
for (var i=0; i<items[1].length; i++){
for(var j=0; j<items[0].length; j++){
if(isCombination){
// clone array to not modify original array
var combination = items[1][i].slice();
combination.push(items[0][j]);
}
else{
var combination = [items[1][i], items[0][j]];
}
combinations.push(combination);
}
}
return combinations
}
else if(items.length > 2){
var last2 = items.slice(-2);
var butLast2 = items.slice(0, items.length - 2);
last2 = allPossibleCombinations(last2, isCombination);
butLast2.push(last2)
var combinations = butLast2;
return allPossibleCombinations(combinations, isCombination=true)
}
}
console.log(allPossibleCombinations(a));
console.log(allPossibleCombinations(a).length);
this was quite complex, and lots of fun, so thanks for asking!
as here we can have m element arrays of arbitary size,
var a = [
["blue", "red"],
[1, 2],
[true, false],
]
function allPossibleCombinations(items, isCombination=false){
// finding all possible combinations of the last 2 items
// remove those 2, add these combinations
// isCombination shows if the last element is itself part of the combination series
if(items.length == 1){
return items[0]
}
else if(items.length == 2){
var combinations =
for (var i=0; i<items[1].length; i++){
for(var j=0; j<items[0].length; j++){
if(isCombination){
// clone array to not modify original array
var combination = items[1][i].slice();
combination.push(items[0][j]);
}
else{
var combination = [items[1][i], items[0][j]];
}
combinations.push(combination);
}
}
return combinations
}
else if(items.length > 2){
var last2 = items.slice(-2);
var butLast2 = items.slice(0, items.length - 2);
last2 = allPossibleCombinations(last2, isCombination);
butLast2.push(last2)
var combinations = butLast2;
return allPossibleCombinations(combinations, isCombination=true)
}
}
console.log(allPossibleCombinations(a));
console.log(allPossibleCombinations(a).length);
var a = [
["blue", "red"],
[1, 2],
[true, false],
]
function allPossibleCombinations(items, isCombination=false){
// finding all possible combinations of the last 2 items
// remove those 2, add these combinations
// isCombination shows if the last element is itself part of the combination series
if(items.length == 1){
return items[0]
}
else if(items.length == 2){
var combinations =
for (var i=0; i<items[1].length; i++){
for(var j=0; j<items[0].length; j++){
if(isCombination){
// clone array to not modify original array
var combination = items[1][i].slice();
combination.push(items[0][j]);
}
else{
var combination = [items[1][i], items[0][j]];
}
combinations.push(combination);
}
}
return combinations
}
else if(items.length > 2){
var last2 = items.slice(-2);
var butLast2 = items.slice(0, items.length - 2);
last2 = allPossibleCombinations(last2, isCombination);
butLast2.push(last2)
var combinations = butLast2;
return allPossibleCombinations(combinations, isCombination=true)
}
}
console.log(allPossibleCombinations(a));
console.log(allPossibleCombinations(a).length);
var a = [
["blue", "red"],
[1, 2],
[true, false],
]
function allPossibleCombinations(items, isCombination=false){
// finding all possible combinations of the last 2 items
// remove those 2, add these combinations
// isCombination shows if the last element is itself part of the combination series
if(items.length == 1){
return items[0]
}
else if(items.length == 2){
var combinations =
for (var i=0; i<items[1].length; i++){
for(var j=0; j<items[0].length; j++){
if(isCombination){
// clone array to not modify original array
var combination = items[1][i].slice();
combination.push(items[0][j]);
}
else{
var combination = [items[1][i], items[0][j]];
}
combinations.push(combination);
}
}
return combinations
}
else if(items.length > 2){
var last2 = items.slice(-2);
var butLast2 = items.slice(0, items.length - 2);
last2 = allPossibleCombinations(last2, isCombination);
butLast2.push(last2)
var combinations = butLast2;
return allPossibleCombinations(combinations, isCombination=true)
}
}
console.log(allPossibleCombinations(a));
console.log(allPossibleCombinations(a).length);
edited Nov 15 '18 at 7:57
answered Nov 15 '18 at 7:50
Aditya ShankarAditya Shankar
37828
37828
pretty impressive, works just fine, thank you.
– Mauro Aguilar
Nov 15 '18 at 9:57
add a comment |
pretty impressive, works just fine, thank you.
– Mauro Aguilar
Nov 15 '18 at 9:57
pretty impressive, works just fine, thank you.
– Mauro Aguilar
Nov 15 '18 at 9:57
pretty impressive, works just fine, thank you.
– Mauro Aguilar
Nov 15 '18 at 9:57
add a comment |