Javascript 如何在节点中的 console.log() 中创建换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49660349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:26:05  来源:igfitidea点击:

how to create line breaks in console.log() in node

javascriptnode.js

提问by DCR

Is there a way to get new lines in console.log when printing multiple objects?

打印多个对象时,有没有办法在 console.log 中获取新行?

Suppose we have console.log(a,b,c)where a, b, and care objects. Is there a way to get a line break between the objects?

假设我们有console.log(a,b,c)在那里abc都是对象。有没有办法在对象之间换行?

I tried console.log(a,'\n',b,'\n',c)but that does not work in node

我试过了,console.log(a,'\n',b,'\n',c)但这在节点中不起作用

采纳答案by DCR

I have no idea why this works in node but the following seems to do the trick:

我不知道为什么这在 node 中有效,但以下似乎可以解决问题:

console.log('',a,'\n',b,'\n',c)

compliments of theBlueFish

蓝鱼的赞美

回答by Ori Drori

Add \n(newline) between them:

\n在它们之间添加(换行符):

console.log({ a: 1 }, '\n', { b: 3 }, '\n', { c: 3 })

回答by S J

Without adding white space at start of new line:-

在新行的开头不添加空格:-

console.log("one\ntwo");

console.log("one\ntwo");

output:-

输出:-

one two

one two

This will add white space at start of new line:-

这将在新行的开头添加空格:-

console.log("one","\n",two");

console.log("one","\n",two");

output:-

输出:-

one two

one two

回答by Ele

An alternative is creating your own logger along with the original logger from JS.

另一种方法是创建您自己的记录器以及来自 JS 的原始记录器。

var originalLogger = console.log;
console.log = function() {
  for (var o of arguments) originalLogger(o);
}

console.log({ a: 1 }, { b: 3 }, { c: 3 })

If you want to avoid any clash with the original logger from JS

如果您想避免与来自 JS 的原始记录器发生任何冲突

console.ownlog = function() {
  for (var o of arguments) console.log(o);
}

console.ownlog({ a: 1 }, { b: 3 }, { c: 3 })

回答by Sua Morales

Another way would be a simple:

另一种方法很简单:

console.log(a);
console.log(b);
console.log(c);

回答by manishk

You need to use \ninside the console.loglike this:

你需要像这样\n在里面使用console.log

console.log('one','\n','two');