猿问

如何冻结文本在 console.log 中的移动

所以我有一个简单的 console.log 脚本来打印这个 

现在当我添加一个字母时http://img.mukewang.com/6440a8520001be0101210056.jpg

请问有什么方法可以冷冻吗?代码

http://img1.mukewang.com/6440a85900018d6403410258.jpg

慕姐4208626
浏览 106回答 2
2回答

开满天机

如果所有单元格都是“空的”,则让所有单元格都包含一个空格字符可能更有意义。看看这里:var Cell_1 = "a";var Cell_2 = " ";var Cell_3 = " ";var Cell_4 = " ";var Cell_5 = " ";var Cell_6 = " ";var Cell_7 = " ";var Cell_8 = " ";var Cell_9 = " ";console.log(Cell_1 + "|" + Cell_2 + "|" + Cell_3 + "\n" +Cell_5 + "|" + Cell_6 + "|" + Cell_6 + "\n" +Cell_7 + "|" + Cell_8 + "|" + Cell_9 + "\n" +)这样你所有的变量都是相同的宽度 - 一个字符。为了将来参考,这里有一些代码可能看起来更好一些:// This is called a 2d array: essentially an array containing other arrays.// Its good for storing grids or tables of information.var cells = [  ['a', ' ', ' '],  [' ', ' ', ' '],  [' ', ' ', ' ']]// This uses Array.reduce() to generate the string.// Google it once you feel more confident :)console.log(  cells.reduce(    (totalString, currentRow) => totalString + currentRow.join('|') + '\n',    ''  ))

沧海一幻觉

这个问题不是很清楚,但我假设你想保持网格对齐,网格有多个单元格,可以包含一个字符,也可以不包含一个字符。问题是空单元格被初始化为""大小为 0 的 (empty string),但是当设置字符时,大小将为 1,因此它会将所有以下单元格移动 1。一个简单的解决方案是对空单元格使用" "(space) 而不是"". 因此,单元格的大小将始终为 1,并且不会移动整个网格。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答