110

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
  • 71,692
  • 44
  • 234
  • 400
Anthony
  • 12,124
  • 13
  • 54
  • 68
  • 8
    Try `console.log(JSON.stringify(arr, null, 1));`. – Sebastian Simon Jan 16 '17 at 04:04
  • So, why not just create your own loop and output it exactly the way you want one at a time? You are in complete control of the output when you do it that way. – jfriend00 Jan 16 '17 at 04:22
  • If I can I would advice changing the marked answer to https://stackoverflow.com/a/41669062/124486 as it's a much better solution if you're on Node v10+ – Evan Carroll Jan 17 '19 at 02:12

5 Answers5

154

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
  • 12,166
  • 9
  • 63
  • 80
Michael Hellein
  • 3,898
  • 1
  • 24
  • 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
  • 26
    @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
  • 7
    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
39

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
  • 7,802
  • 5
  • 22
  • 36
16

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
  • 71,692
  • 44
  • 234
  • 400
squiroid
  • 13,519
  • 5
  • 45
  • 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
  • `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
9

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

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

Stanislav
  • 418
  • 5
  • 8
5

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

djones
  • 2,363
  • 1
  • 16
  • 21
  • 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