我试图记录一个长数组,这样我就可以在我的终端快速复制它。但是,如果我尝试记录数组,它看起来像:
['item', 'item', >>more items<<< ... 399 more items ]
如何记录整个数组,以便能够快速复制它?
console.table
在 Node v10 + ,以及所有现代网络浏览器中,您可以使用 console.table(),它将输出一个漂亮的 utf8表,其中每一行代表数组的一个元素。
console.table()
> console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']); ┌─────────┬─────┐ │ (index) │ a │ ├─────────┼─────┤ │ 0 │ 1 │ │ 1 │ 'Z' │ └─────────┴─────┘
myArray.forEach(item => console.log(item))怎么了?
myArray.forEach(item => console.log(item))
maxArrayLength
There are a few methods all of which require setting maxArrayLength which otherwise defaults to 100.
将覆盖作为 console.dir的选项提供
console.dir
console.dir(myArry, {'maxArrayLength': null});
Set util.inspect.defaultOptions.maxArrayLength = null; which will impact all calls to console.log and util.format
util.inspect.defaultOptions.maxArrayLength = null;
console.log
util.format
Call util.inspect yourself with options.
util.inspect
const util = require('util') console.log(util.inspect(array, { maxArrayLength: null }))
刚刚发现,选项 maxArrayLength与 console.dir也工作得很好:
console.dir(array, {depth: null, colors: true, maxArrayLength: null});
迈克尔•海伦(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()