打印数组时奇怪的node.js console.log()输出行为 [英] Weird node.js console.log() output behaviour when printing arrays

查看:74
本文介绍了打印数组时奇怪的node.js console.log()输出行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VSCode(Mac OS)中有以下一些简单的代码,它们在node.js v14.13.1环境中运行:

I have the following, somewhat simple code in VSCode(Mac OS) which I run in node.js v14.13.1 environment:

let fruits = ['Apple', 'Banana'];
let fruits1 = ['Apple1', 'Banana1'];
console.log(fruits);
console.log(fruits1);

输出行为对我来说似乎很奇怪,它可以打印:

The output behaviour seems very weird to me, it prints either:

(2) ['Apple', 'Banana']

(2) ['Apple', 'Banana']
(2) ['Apple1', 'Banana1']

(2) ['Apple', 'Banana']
Canceled

我找不到任何特定的打印模式(除了前两个输出比第三个输出更频繁地打印),因此似乎随机决定"输出什么.

I couldn't find any particular pattern of printing (except for first two outputs are printed more often than the third one), so it seems as if it 'randomly decides' what to output.

但是,此代码在通过Mac终端( node my_file.js )执行时始终能按预期输出,即:

This code, however, outputs always as expected when executed via Mac terminal(node my_file.js), i.e.:

[ 'Apple', 'Banana' ]
[ 'Apple1', 'Banana1' ]

是某种VSCode错误,还是用console.log()打印数组(用字符串和整数工作就可以了)是我不考虑的吗?

Is that some kind of VSCode bug, or there is something about printing arrays with console.log() (with strings and integers it works just fine) what I don't take into account?

推荐答案

您可以尝试的一种解决方案是,在执行console.log命令时将数组转换为字符串:

One solution you could try is to convert the array to a string when doing the console.log command:

let fruits = [['Apple', 'Banana'],['Apple', 'Banana']];
console.log(fruits.toString());  // Apple,Banana,Apple,Banana
console.log('' + fruits);  // Apple,Banana,Apple,Banana
console.log(JSON.stringify(fruits));  // [["Apple","Banana"],["Apple","Banana"]]

我喜欢最后一个,因为即使是多维数组,它也可以使该数组显示在方括号内.

I like the last one because it keeps this array displayed within brackets, even with multidimensional arrays.

但是,我发现的一个问题是使用正则表达式时,转换为字符串时会丢失附加输出:

However, one problem I found with this is when working with regular expression, there is addition output lost when converting to a string:

let str = 'xyz';
let pat = /y/;
let res = str.match(pat);
console.log(res);  // ['y', index: 1, input: 'xyz', groups: undefined]
console.log(res.toString());  // y
console.log('' + res);  // y
console.log(JSON.stringify(res));  // ["y"]

我相信可能会发生此错误,因为在将输出发送到调试控制台之前调试器正在停止?因此,对于另一种解决方案,可以在代码末尾添加一个空行,然后在其中添加一个断点.这似乎允许在对数组执行多个console.logs时生成所有输出.或者,在代码末尾添加:

I believe this error may be occurring because the debugger is stopping before the output is sent to the debug console? Therefore For another solution, either add an empty line at the end of your code and add a breakpoint there; this seems to allow all the output to be generated when doing a multiple console.logs on arrays. Or, at the end of the code, add:

setTimeout(() => { }, 1000)

这似乎还增加了足够的时间来正确输出所有的console.log数组.前者的优点是,您可以在调试控制台中扩展数组详细信息,直到继续执行代码结尾为止,但是缺点是您仍然必须选择继续执行代码才能结束代码.后者还允许您在输出中扩展对象的详细信息,但只能在计时器的持续时间内进行.

Which also seems to be adding enough time to properly output all the console.log of arrays. The advantage to the former is, you can expand the array details in the debug console, until you continue to the end of the code, but the con is you still have to chose to continue the code for it to end. The latter also allows you to expand the details of the object in the output, but only for the duration of the timer.

我还发现,您可以将它们之一添加到launch.json文件中(在适当的节点配置对象中,并且不要忘记在该行之前和/或该行的末尾添加一个公共符号.这些行中的哪一条取决于您要插入的位置):

I also found, you can add either of these to your launch.json file (within the appropriate node configuration object, and don't forget to add a common to either the end the line before and/or the the end of either of these lines depending on where you are inserting):

"console": "integratedTerminal"

或者:

"console": "externalTerminal"

这些将输出发送到VS Code终端窗格或外部cmd窗口.

These send the output to either the VS Code terminal pane or an external cmd window.

最后我也找到了这两个控制台命令中的任何一个,您可以将其添加到launch.json中:

And finally I also found instead of either of these console command, you can add this to the launch.json:

"outputCapture": "std"

这会将输出保留在调试控制台中.颜色和颜色看起来有些不同其他一些消息,但似乎可以成功输出数组.

This will keep the output in the debug console. It looks a bit different with color & some other messages, but seems to successfully output arrays.

这篇关于打印数组时奇怪的node.js console.log()输出行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆