Anthony
Anthony

Reputation: 14269

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

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?

Upvotes: 151

Views: 92937

Answers (6)

Tristan Trim
Tristan Trim

Reputation: 96

None of the other answers worked for me. I used:

console.log(MY_ARRAY.reduce((l,r)=>l+", "+r));

The "reduce" function for those unfamiliar repeatedly calls a function with pairs of elements of the array until only one element remains, so in this example, I'm repeatedly adding ", " and the next element to a string.

Upvotes: 0

Michael Hellein
Michael Hellein

Reputation: 4568

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 }))
    

Upvotes: 220

Big Money
Big Money

Reputation: 10048

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.

Upvotes: 53

squiroid
squiroid

Reputation: 14017

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' │
└─────────┴─────┘

Upvotes: 26

Stanislav
Stanislav

Reputation: 516

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

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

Upvotes: 11

David Jones
David Jones

Reputation: 3342

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

Upvotes: 8

Related Questions