-2

I have an array of 16 arrays like this:

[['one', 'blue'], ['one', 'red'], ['two', 'blue'], ['two', 'red'], ...]

and I'm looking for Javascript to return every combination (not permutation, because I don't want repeats in different orders) of THREE of them like this:

[[['one', 'blue'], ['one', 'red'], ['two', 'blue']],
 [['one', 'blue'], ['one', 'red'], ['two', 'red']],
 [['one', 'blue'], ['two', 'blue'], ['two', 'red']],
 [['one', 'red'], ['two', 'blue'], ['two', 'red']], ...]

I can do this in Python using itertools, but I don't know how to in Javascript.

Thanks!

Brian
  • 53
  • 6
  • Does this answer your question? [javascript permutation generator with permutation length parameter](https://stackoverflow.com/questions/23305747/javascript-permutation-generator-with-permutation-length-parameter) – Heretic Monkey Jun 24 '23 at 00:52

2 Answers2

1

You could use three nested for loops.

const arrays = [['one', 'blue'], ['one', 'red'], ['two', 'blue'], ['two', 'red']];

const result = [];
for (let i = 0; i < arrays.length - 2; ++i) {
  for (let j = i + 1; j < arrays.length - 1; ++j) {
    for (let k = j + 1; k < arrays.length; ++k) {
      result.push([arrays[i], arrays[j], arrays[k]]);
    }
  }
}

console.log(result);

Or, if you need a shallow copy instead of references:

const arrays = [['one', 'blue'], ['one', 'red'], ['two', 'blue'], ['two', 'red']];

const result = [];
for (let i = 0; i < arrays.length - 2; ++i) {
  for (let j = i + 1; j < arrays.length - 1; ++j) {
    for (let k = j + 1; k < arrays.length; ++k) {
      result.push([[...arrays[i]], [...arrays[j]], [...arrays[k]]]);
    }
  }
}

console.log(result);
jabaa
  • 5,844
  • 3
  • 9
  • 30
0

You want to generate a list of permutations of a fixed length.

There are many algorithms out there for recursively building a list of permutations.

I modified one that was easy to find, with proper searching. I called stucturedClone to grab a deep copy of each result item.

const arr = [
  ['one', 'blue'],
  ['one', 'red'],
  ['two', 'blue'],
  ['two', 'red']
];

// Based on: https://stackoverflow.com/a/59370795/1762224
const getPermutations = (list, maxLen) => {
  const permute = (list, maxLen) => {
    if (maxLen === 0) return [[]];
    const result = [];
    for (let i = 0; i < list.length; i++) {
      let
        copy = [...list],
        head = copy.splice(i, 1),
        rest = permute(copy, maxLen - 1);
      for (let j = 0; j < rest.length; j++) {
        result.push(structuredClone([...head, ...rest[j]]));
      }
    }
    return result;
  }
  return permute(list, maxLen);
}

const permutations = getPermutations(arr, 3);

console.log(permutations);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Results:

[
  [ ["one", "blue"] , ["one", "red"]  , ["two", "blue"] ]
  [ ["one", "blue"] , ["one", "red"]  , ["two", "red"]  ]
  [ ["one", "blue"] , ["two", "blue"] , ["one", "red"]  ]
  [ ["one", "blue"] , ["two", "blue"] , ["two", "red"]  ]
  [ ["one", "blue"] , ["two", "red"]  , ["one", "red"]  ]
  [ ["one", "blue"] , ["two", "red"]  , ["two", "blue"] ]
  [ ["one", "red"]  , ["one", "blue"] , ["two", "blue"] ]
  [ ["one", "red"]  , ["one", "blue"] , ["two", "red"]  ]
  [ ["one", "red"]  , ["two", "blue"] , ["one", "blue"] ]
  [ ["one", "red"]  , ["two", "blue"] , ["two", "red"]  ]
  [ ["one", "red"]  , ["two", "red"]  , ["one", "blue"] ]
  [ ["one", "red"]  , ["two", "red"]  , ["two", "blue"] ]
  [ ["two", "blue"] , ["one", "blue"] , ["one", "red"]  ]
  [ ["two", "blue"] , ["one", "blue"] , ["two", "red"]  ]
  [ ["two", "blue"] , ["one", "red"]  , ["one", "blue"] ]
  [ ["two", "blue"] , ["one", "red"]  , ["two", "red"]  ]
  [ ["two", "blue"] , ["two", "red"]  , ["one", "blue"] ]
  [ ["two", "blue"] , ["two", "red"]  , ["one", "red"]  ]
  [ ["two", "red"]  , ["one", "blue"] , ["one", "red"]  ]
  [ ["two", "red"]  , ["one", "blue"] , ["two", "blue"] ]
  [ ["two", "red"]  , ["one", "red"]  , ["one", "blue"] ]
  [ ["two", "red"]  , ["one", "red"]  , ["two", "blue"] ]
  [ ["two", "red"]  , ["two", "blue"] , ["one", "blue"] ]
  [ ["two", "red"]  , ["two", "blue"] , ["one", "red"]  ]
]
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • 2
    From the question: _"not permutation, because I don't want repeats in different orders"_ Your result contains `[ ["one", "blue"] , ["one", "red"] , ["two", "blue"] ]` and `[ ["two", "blue"] , ["one", "blue"] , ["one", "red"] ]` – jabaa Jun 24 '23 at 00:51