node.js 转储整个数组:console.log 和 console.dir 输出“... NUM more items]”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41669039/
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-09-02 17:55:31  来源:igfitidea点击:

Dumping whole array: console.log and console.dir output "... NUM more items]"

arraysnode.jsv8dumpconsole.log

提问by Anthony

I am trying to log a long array so I can copy it quickly in my terminal. However, if I try and log the array it looks like:

我正在尝试记录一个长数组,以便我可以在终端中快速复制它。但是,如果我尝试记录数组,它看起来像:

['item',
 'item',
  >>more items<<<
  ... 399 more items ]

How can I log the entire array so I can copy it really quickly?

如何记录整个阵列,以便我可以快速复制它?

回答by Michael Hellein

Setting maxArrayLength

环境 maxArrayLength

There are a few methods all of which require setting maxArrayLengthwhich otherwise defaults to 100.

有几种方法都需要设置maxArrayLength,否则默认为 100。

  1. Provide the override as an option to console.dir

    console.dir(myArry, {'maxArrayLength': null});
    
  2. Set util.inspect.defaultOptions.maxArrayLength = null;which will impact all calls to console.logand util.format

  3. Call util.inspectyourself with options.

    const util = require('util')
    console.log(util.inspect(array, { maxArrayLength: null }))
    
  1. 提供覆盖作为选项 console.dir

    console.dir(myArry, {'maxArrayLength': null});
    
  2. 设置util.inspect.defaultOptions.maxArrayLength = null;将影响对console.log和的所有调用util.format

  3. 呼叫util.inspect自己与选项

    const util = require('util')
    console.log(util.inspect(array, { maxArrayLength: null }))
    

回答by Big Money

Michael Hellein's answer didn't work for me, but a close version did:

Michael Hellein 的回答对我不起作用,但一个接近的版本确实如此:

console.dir(myArray, {'maxArrayLength': null})

This was the only solution that worked for me as JSON.stringify()was too ugly for my needs and I didn't need to write the code to print it out one at a time.

这是唯一对我JSON.stringify()有用的解决方案,因为它对我的需求来说太难看了,而且我不需要编写代码来一次打印一个。

回答by squiroid

Using console.table

使用 console.table

Available in Node v10+, and all modern web-browsers, you can use console.table()instead, which will output a beautiful utf8 table where each row represents an element of the array.

在 Node v10+和所有现代 web-browsers 中可用,您可以改用console.table()它,它将输出一个漂亮的 utf8 表,其中每一行代表数组的一个元素。

> console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']);

┌─────────┬─────┐
│ (index) │  a  │
├─────────┼─────┤
│    0    │  1  │
│    1    │ 'Z' │
└─────────┴─────┘

回答by Stanislav

Just found that option maxArrayLengthworks well with console.dirtoo:

刚刚发现该选项也maxArrayLength适用于console.dir

console.dir(array, {depth: null, colors: true, maxArrayLength: null});

console.dir(array, {depth: null, colors: true, maxArrayLength: null});

回答by djones

What's wrong with myArray.forEach(item => console.log(item))?

怎么了myArray.forEach(item => console.log(item))