Node.js formatted console output

后端 未结 6 599
说谎
说谎 2021-01-31 13:24

Is there a simple built-in way to output formatted data to console in Node.js?

Indent, align field to left or right, add leading zeros?

6条回答
  •  失恋的感觉
    2021-01-31 14:07

    If the data is tabular, then the simplest way would be to do it with console.table

    https://nodejs.org/dist/latest-v10.x/docs/api/console.html#console_console_table_tabulardata_properties

    This is the code.

    console.table(
      COMMANDS.map(command => {
        return {
          "Long Option": command.long_option,
          "Short Option": command.short_option,
          Description: command.description
        };
      })
    );
    

    You don't need external libraries for it. Here is sample output. You only need to pass an array object.

    Not only in Nodejs, but it also works in chrome.

    https://developer.mozilla.org/en-US/docs/Web/API/Console/table

提交回复
热议问题