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

52,407

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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

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

Solution 5

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

Share:
52,407

Related videos on Youtube

Anthony
Author by

Anthony

Updated on February 06, 2020

Comments

  • Anthony
    Anthony over 4 years

    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?

    • Sebastian Simon
      Sebastian Simon over 7 years
      Try console.log(JSON.stringify(arr, null, 1));.
    • jfriend00
      jfriend00 over 7 years
      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.
    • Evan Carroll
      Evan Carroll over 5 years
      If I can I would advice changing the marked answer to stackoverflow.com/a/41669062/124486 as it's a much better solution if you're on Node v10+
  • squiroid
    squiroid over 7 years
  • Anthony
    Anthony over 7 years
    Don't want a different package sorry, hoping to do this with native node
  • Anthony
    Anthony over 7 years
    I want to add these items to an existing array.
  • George
    George almost 7 years
    if you want to add one array to another, why not use Array.concat?
  • Magnus Bodin
    Magnus Bodin over 6 years
    Specifically I like also to do console.log(util.inspect(arrayofObjects, {maxArrayLength: null, depth:null })) to go deep into objects as well.
  • Anthony
    Anthony almost 6 years
    Just had a chance to check this, saving as the proper answer
  • Evan Carroll
    Evan Carroll over 5 years
    @Antoine it's now in Node v10 you may consider reassessing the question. This is a better method =)
  • Big Money
    Big Money over 5 years
    @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.
  • Michael Hellein
    Michael Hellein over 5 years
    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.
  • Lincoln
    Lincoln over 5 years
    same here, the other option would print the same thing
  • nonopolarity
    nonopolarity about 3 years
    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"
  • Kamafeather
    Kamafeather about 3 years
    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.