Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 392
Anthony Avatar asked Jan 16 '17 03:01

Anthony


People also ask

What is the difference between console log and console dir?

The console method log() displays the toString representation of any object passed to it. The Console method dir() displays an interactive list of the properties of the specified JavaScript object.

What is console Dir in JavaScript?

dir() The method console. dir() displays an interactive list of the properties of the specified JavaScript object. The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects.

What is the use of console log () method in node JS?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.


2 Answers

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 })) 
like image 133
Michael Hellein Avatar answered Sep 22 '22 17:09

Michael Hellein


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.

like image 24
Big Money Avatar answered Sep 20 '22 17:09

Big Money