NodeJs如何在控制台中显示结构化表

时间:2017-11-10 17:31:32

标签: node.js

是否可以在不安装新模块的情况下使用?

enter image description here

我在这里使用cli-table模块..

我想要类似的输出而不安装它。有一种简单而且更好的方法吗?

4 个答案:

答案 0 :(得分:5)

node.js版本v10.16.0,您可以使用console.table API而不安装任何节点模块。

  const structDatas = [
    { handler: 'http', endpoint: 'http://localhost:3000/path', method: 'ALL' },
    { handler: 'event', endpoint: 'http://localhost:3000/event', method: 'POST' },
    { handler: 'GCS', endpoint: 'http://localhost:3000/GCS', method: 'POST' }
  ];
  console.table(structDatas);

在终端中的标准输出是这样的:

enter image description here

答案 1 :(得分:0)

var values = [];
Array.forEach(function(b, i) {
    values.push(
    {
        'Symbol': b.a,
        'BID': b.b,
        'CONVERT1': b.c,
        'Buy': b.d,
        'Sell': b.e,
        'Convert2': b.f
    });
});
console.table(values);

Result Console

答案 2 :(得分:0)

我喜欢这个软件包,用于在终端上更新文本,而不是添加新行:https://github.com/ivanseidel/node-draftlog

答案 3 :(得分:-1)

console.table(["apples", "oranges", "bananas"]);

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