为了账号安全,请及时绑定邮箱和手机立即绑定

如何在JavaScript中动态创建二维(2D)字符串数组

如何在JavaScript中动态创建二维(2D)字符串数组

慕后森 2021-05-09 16:43:41
如果这个词是ABCA[0][0]="AA"   A[0][1]="AB"   A[0][2]="AC"A[1][0]="BA"   A[1][1]="BB"   A[1][2]="BC"A[2][0]="CA"   A[2][1]="CB"   A[2][2]="CC"使用for,字符串或数组方法。
查看完整描述

3 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

const a = [..."ABC"];


console.log(

  a.map(l => a.map(c => l + c))

);


查看完整回答
反对 回复 2021-05-27
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

奇怪的要求。这是你想要的?


const word = "ABC";

const letters = word.split("");

const array = [];

letters.forEach((letter1,index1) => {

  letters.forEach((letter2,index2) => {

     if (!array[index1]) {

        array[index1] = [];

     }

     array[index1][index2] = letter1+letter2;

  });

});

console.log(array);


查看完整回答
反对 回复 2021-05-27
?
慕哥9229398

TA贡献1877条经验 获得超6个赞

更新:


使用较旧的Javascript的另一个版本。另外,使用以下更实用的方法查看Asaf的解决方案,这是非常优雅的。


var word = "ABC";

var letters = word.split("");

var array = [];

for(var index1 = 0;index1!==letters.length;index1++) {

  for(var index2 = 0;index2!==letters.length;index2++) {

     if (!array[index1]) {

        array[index1] = [];

     }

     array[index1][index2] = letters[index1]+letters[index2];

  }

}

console.log(array);


查看完整回答
反对 回复 2021-05-27
  • 3 回答
  • 0 关注
  • 207 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信