js 数组全排列算法

      /**
       * @function 数组排列组合
       * [1,2,3] => 123 132 213 231 321 312
       */
      function permutationCombination(arr) {
        if (arr.length === 1) {
          return [arr];
        }

        const res = [];
        arr.forEach((val, index) => {
          const childArr = [...arr];
          childArr.splice(index, 1);

          res.push(
            ...permutationCombination(childArr).map(item => [val, ...item])
          );
        });

        return res;
      }

你可能感兴趣的:(javascript前端算法)