首页 > 解决方案 > 洗牌列 2d 数组 javascript/typescript

问题描述

我正在尝试洗牌我的二维数组,但只有列。我有一个函数,它使用用户给出的字符串生成一个由字母和黑色正方形组成的二维数组(当我按下 0 时):

creerGrilleHaut(inputCitation: string) {
    this.citation = inputCitation.trim();

    for (var i = 0; i < this.citation.length; i++) {
      if ([' ', ',', '-', '.', "'"].indexOf(this.citation[i]) >= 0) {
        this.monArray.push(0);
      } else {
        this.monArray.push(this.citation[i].toUpperCase());
      }
    }

    while (this.monArray.length) {
      var monTableau = (this.monArray.splice(0, 13));
      this.monArray2DHaut.push(monTableau);


    }

    this.nbLignes = this.citation.length / this.nbColonnes;
    if (this.citation.length % this.nbColonnes != 0) {
      this.nbLignes++;
    }
}

结果:

L   E       C   H   A   T       N       E   S   T
P   A   S       L   A       L   E   S       S
O   U   R   I   S       D   A   N   S   E   N    T

我随机想要的:

L   U   R   C   S       D   L   E   S   E   N   T
O   E       I   L   A       A   N       E   S   T
L   U   S       H   A   T       N   S       S   

标签: javascriptarraystypescriptshuffle

解决方案


推荐阅读