📜  Node.js console.group() 方法

📅  最后修改于: 2022-05-13 01:56:36.999000             🧑  作者: Mango

Node.js console.group() 方法

控制台。 group()方法是控制台模块的内置应用程序编程接口,用于以分组样式打印传递的内容。它表示消息组的开始。要结束这个组,我们可以使用控制台。 groupEnd()方法。

句法:

console.group([label]);

参数:此方法只接受一个参数,如上所述,如下所述:

  1. label:您在控制台上分组的组的标签。这个参数不是必须的,它是可选的,可以在控制台运行。不传递标签参数的 group() 方法。

参数值:

  1. 类型:字符串
  2. 必需:可选

返回值:此方法不返回任何内容,而是使用标签对控制台上的内容进行分组,否则仅提供不带标签的内容。

下面的例子说明了控制台的使用。 Node.js 中的group()方法。

示例 1:

index.js
// Node.js program to demonstrate the 
// console.group() method 
// This code example demonstrate the
// method without parameter
  
// Accessing console module 
const console = require('console'); 
  
console.log("GeeksforGeeks");
console.log();
  
// Creating  First Group
console.group();
  
// Printing First Statement of Group 
console.log("1st print from the first group");
  
// Printing Second Statement of Group 
console.log("2nd print from the first group");
  
// Ending  the group 
console.groupEnd();
console.log();


index.js
// Node.js program to demonstrate the 
// console.group() method 
  
// This code example demonstrate the method
// with parameter and nested groups
  
// Accessing console module 
const console = require('console');
  
console.log("GeeksforGeeks ");
  
console.log("==========================");
console.log();
  
// Creating First group
console.group("This is the starting main group");
console.log();
  
// Printing First statement of Group 
console.log("1st group and not from any nested group");
console.log();
  
// Creating nested group in First group 
console.group("Starting of the 1st Nested group");
  
// Printing 1st line of nested group
console.log("Hello Geeks for Geeks from "
    + "1st Main Nested group");
  
// Printing 2nd line of nested group 
console.log("Computer Science Portal(GeeksforGeeks)"
    + " from 1st Main Nested group");
  
// Ending of the first nested group
console.groupEnd();
  
console.log();
console.log();
  
// Creating second nested group 
console.group("Starting of the 2nd Main Nested group");
  
// Printing 1st line of second nested group
console.log("Hello from 2nd Main Nested group");
  
// Printing 2nd line of second nested group
console.log("Hello from 2nd Main Nested group");
  
// Ending of the second nested group
console.groupEnd();
console.log();
  
// Printing last line of the group
console.log("Now Main group will end");
  
console.log();
  
// Calling groupEnd() method (It is used to 
// end the group for more understanding 
// the group method) 
console.groupEnd();
  
// Printing from outside the Main group
console.log("This the Hello from GeeksforGeeks"
    + " from outside the Main Group");
  
console.log();
console.log("===========================");


使用以下命令运行index.js文件:

node index.js

控制台输出:

GeeksforGeeks

 1st print from the first group
 2nd print from the first group

示例 2:

index.js

// Node.js program to demonstrate the 
// console.group() method 
  
// This code example demonstrate the method
// with parameter and nested groups
  
// Accessing console module 
const console = require('console');
  
console.log("GeeksforGeeks ");
  
console.log("==========================");
console.log();
  
// Creating First group
console.group("This is the starting main group");
console.log();
  
// Printing First statement of Group 
console.log("1st group and not from any nested group");
console.log();
  
// Creating nested group in First group 
console.group("Starting of the 1st Nested group");
  
// Printing 1st line of nested group
console.log("Hello Geeks for Geeks from "
    + "1st Main Nested group");
  
// Printing 2nd line of nested group 
console.log("Computer Science Portal(GeeksforGeeks)"
    + " from 1st Main Nested group");
  
// Ending of the first nested group
console.groupEnd();
  
console.log();
console.log();
  
// Creating second nested group 
console.group("Starting of the 2nd Main Nested group");
  
// Printing 1st line of second nested group
console.log("Hello from 2nd Main Nested group");
  
// Printing 2nd line of second nested group
console.log("Hello from 2nd Main Nested group");
  
// Ending of the second nested group
console.groupEnd();
console.log();
  
// Printing last line of the group
console.log("Now Main group will end");
  
console.log();
  
// Calling groupEnd() method (It is used to 
// end the group for more understanding 
// the group method) 
console.groupEnd();
  
// Printing from outside the Main group
console.log("This the Hello from GeeksforGeeks"
    + " from outside the Main Group");
  
console.log();
console.log("===========================");

控制台输出:

GeeksforGeeks 
==========================

This is the starting main group
  
  1st group and not from any nested group
  
  Starting of the 1st Nested group
    Hello Geeks for Geeks from 1st Main Nested group
    Computer Science Portal(GeeksforGeeks) 
      from 1st Main Nested group
  
  
  Starting of the 2nd Main Nested group
    Hello from 2nd Main Nested group
    Hello from 2nd Main Nested group
  
  Now Main group will end
  
This the Hello from GeeksforGeeks from 
  outside the Main Group        

===========================

参考: https://nodejs.org/api/console.html#console_console_group_label