1

suppose we have this array :

let a = ["a", "b", "c"]

I need some combination like below:

[["a"], ["b"], ["c"], ["a", "b"], ["a", "c"], ["b", "c"], ["b", "a"], ["c", "a"], ["c","b"], ["a", "b", "c"], ...]

const a = ["a", "b", "c"];

function perm(xs) {
  let ret = [];

  for (let i = 0; i < xs.length; i = i + 1) {
    let rest = perm(xs.slice(0, i).concat(xs.slice(i + 1)));

    if (!rest.length) {
      ret.push([xs[i]])
    } else {
      for (let j = 0; j < rest.length; j = j + 1) {
        ret.push([xs[i]].concat(rest[j]))
      }
    }
  }
  return ret;
}
console.log(perm(a));

feel free to edit this question and if this is a similar question pls duplicate the question .strong text

Argee
  • 1,216
  • 1
  • 12
  • 22
Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46

1 Answers1

0

after searching a while, I foundout there is a function :

function combination(A, comb = [], result = [comb]) {
        for (var i = 0; i < A.length; i++)
            result = result.concat(combination(A.slice(0, i).concat(A.slice(i + 1)),            comb.concat(A[i])));
        return result;
    }
    
 console.log(combination(["q", "a", "w"]))

and you can delete the first index with .slice(1, array.length)

Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46