简体   繁体   English

如何在 JavaScript 中使用 console.log() 在同一行打印?

[英]How to print on same line using console.log() in JavaScript?

I want to print a list (var lst=[1,2,3,4,5]) using a loop on the same line.我想在同一行上使用循环打印一个列表 (var lst=[1,2,3,4,5])。 Can I do that in JavaScript ?我可以在 JavaScript 中做到这一点吗?

Well you can achieve this by doing this.那么你可以通过这样做来实现这一点。 Make an empty string variable and then concatenate each array value to it, then print it out.创建一个空字符串变量,然后将每个数组值连接到它,然后将其打印出来。 This will work这将工作

 var lst = [1,2,3,4,5];
  var output = "";

for(var i =0; i <= lst.length; i++){
   output += lst[i] + " ";
}
console.log(output);

Console.log doesn't allow you to print in the same line. Console.log 不允许您在同一行中打印。 However the task that you are trying to do can be done using the below:但是,您尝试执行的任务可以使用以下方法完成:

console.log(lst.join(" "))

Output: 1 2 3 4 5输出:1 2 3 4 5

The exact behavior of console.log is not specified by ECMAScript, so it varies from implementation to implementation, but it is intended to be used for logging events. console.log的确切行为不是由 ECMAScript 指定的,因此它因实现而异,但它旨在用于记录事件。 The fact that each event usually becomes a single line in a web browser's console window led it to be the Node.js equivalent of "println", but outside the context of a specific implementation its behavior is not predictable.每个事件通常在 Web 浏览器的控制台窗口中变成一行这一事实导致它成为 Node.js 中的“println”等价物,但在特定实现的上下文之外,其行为是不可预测的。

You can pass multiple parameters to log , in which case most (but not all) implementations will print them all out on the same line separated by whitespace.您可以将多个参数传递给log ,在这种情况下,大多数(但不是全部)实现会将它们全部打印在由空格分隔的同一行上。 So if you're using one of those implementations, and you have a list and just want all the elements on one line separated by space, you can use apply to call the method as if each element of the list was a separate argument:因此,如果您正在使用这些实现之一,并且您有一个列表,并且只希望一行中的所有元素以空格分隔,则可以使用apply调用该方法,就好像列表中的每个元素都是一个单独的参数:

console.log.apply(console, lst);

Caveat: if the first argument is a string that contains what look like Formatter format control sequences ( %s , etc.), it will be parsed as such and the remaining arguments used to fill in those slots.警告:如果第一个参数是一个字符串,其中包含看起来像Formatter格式控制序列( %s等)的内容,它将被解析为这样,其余参数用于填充这些插槽。

But the most reliable way to achieve the desired result is to build a single string yourself representing what you want the final line to look like, and then call console.log exactly once with that string as the argument.但实现预期结果的最可靠方法是自己构建一个字符串,代表您希望最后一行的外观,然后使用该字符串作为参数调用console.log一次。

You could use Function#apply or spread syntax ... .您可以使用Function#apply传播语法...

 var list = [1, 2, 3, 4, 5]; console.log.apply(null, list); // traditional console.log(...list); // ES6

In NodeJS and as long as you didn't change your standard output you can use process.stdout.write("Something here") as the standard output of your application is terminal (console).在 NodeJS 中,只要您没有更改标准输出,就可以使用process.stdout.write("Something here")作为应用程序的标准输出是终端(控制台)。

process.stdout.write("Hello ")
process.stdout.write("World")
process.stdout.write("!")

Hello world!你好,世界!

You could do it easily by using the join() function which can be used on any array in JavaScript!您可以使用join()函数轻松完成此操作,该函数可用于 JavaScript 中的任何数组!

For example:例如:

var lst = ["check","word","3","could","hello"]
console.log(lst.join())

source-code:源代码:
https://jsfiddle.net/Lpdurxsb/ https://jsfiddle.net/Lpdurxsb/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM