Array Arrangement Permutation

前端 未结 4 558
半阙折子戏
半阙折子戏 2021-01-26 18:09

Looking for a way to sequentially find different arrangement possibilities of an array. I only care about adding them sequentially, doesn\'t need skip or shuffle values.

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 18:15

    You can easily reduce the array by slicing the array to the current index.

    var inputArray = ['a', 'b', 'c', 'd', 'e', 'f'];
    
    var outputArray = inputArray.reduce(function(result, item, index, arr) {
      return result.concat(arr.slice(0, index + 1).join(''));
    }, []);
    
    
    document.body.innerHTML = '
    ' + outputArray.join('\n') + '
    ';

    Note: I am still not sure what you mean by "find different arrangement possibilities of an array".

提交回复
热议问题