140

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?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Anthony
  • 13,434
  • 14
  • 60
  • 80

5 Answers5

201

Setting maxArrayLength

There are a few methods all of which require setting maxArrayLength which otherwise defaults to 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.log and util.format

  3. Call util.inspect yourself with options.

    const util = require('util')
    console.log(util.inspect(array, { maxArrayLength: null }))
    
Govind Rai
  • 14,406
  • 9
  • 72
  • 83
Michael Hellein
  • 4,378
  • 1
  • 25
  • 21
  • 3
    Specifically I like also to do `console.log(util.inspect(arrayofObjects, {maxArrayLength: null, depth:null }))` to go deep into objects as well. – Magnus Bodin Feb 11 '18 at 07:46
  • Just had a chance to check this, saving as the proper answer – Anthony Aug 30 '18 at 17:50
  • 27
    @MichaelHellein passing `maxArrayLength` as an option to console.log just ends up printing it as well. `... 267 more items ] { maxArrayLength: 500 }`. I had to use `console.dir` instead of `console.log` in order to get it to work. – Big Money Feb 06 '19 at 00:35
  • 9
    Just a note for posterity: the answer that I gave has been so significantly edited, I don't think it's fair call it mine anymore. I would have preferred that such a substantial edit were provided as a separate answer. – Michael Hellein Mar 21 '19 at 21:22
  • @MichaelHellein, you’re absolutely right. The original answer didn’t suggest `console.dir` or setting the global defaults, _at all_. Such an aggressive edit shouldn’t be made. – Константин Ван Dec 15 '22 at 13:16
  • It's worth noting that if you're here because you want to print _everything_, you will also want to set `maxStringLength` to `null`, or you'll have the same `...` problem with long strings. – uckelman Apr 16 '23 at 13:39
49

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

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.

Big Money
  • 9,139
  • 6
  • 26
  • 37
23

Using 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.

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

┌─────────┬─────┐
│ (index) │  a  │
├─────────┼─────┤
│    0    │  1  │
│    1    │ 'Z' │
└─────────┴─────┘
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
squiroid
  • 13,809
  • 6
  • 47
  • 67
  • 1
    Here is the support. https://developer.mozilla.org/en-US/docs/Web/API/Console/table – squiroid Jan 16 '17 at 03:38
  • Don't want a different package sorry, hoping to do this with native node – Anthony Jan 16 '17 at 03:45
  • @Antoine it's now in Node v10 you may consider reassessing the question. This is a better method =) – Evan Carroll Jan 17 '19 at 15:22
  • 1
    `console.table` function is pretty useful, but the OP asked for a function that prints out and allow them to copy the output quickly; supposedly still in JS format (to paste somewhere else or to cache the result). Hence `console.dir()` from the top answer satisfies better the question. – Kamafeather Apr 05 '21 at 06:44
10

Just found that option maxArrayLength works well with console.dir too:

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

Stanislav
  • 486
  • 5
  • 9
8

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

David Jones
  • 2,879
  • 2
  • 18
  • 23
  • I want to add these items to an existing array. – Anthony Jan 16 '17 at 04:30
  • if you want to add one array to another, why not use [Array.concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat)? – George Jul 05 '17 at 22:59
  • I think he may want to existing structure, rather than item by item. It also is a problem when each item is an array which is "too long" – nonopolarity Apr 04 '21 at 16:11