关于javascript:js-数组全排列算法

8次阅读

共计 288 个字符,预计需要花费 1 分钟才能阅读完成。

      /**
       * @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;
      }
正文完
 0