Dumping whole array: console.log and console.dir output "... NUM more items]"
Solution 1
Setting maxArrayLength
There are a few methods all of which require setting maxArrayLength
which otherwise defaults to 100.
-
Provide the override as an option to
console.dir
console.dir(myArry, {'maxArrayLength': null});
Set
util.inspect.defaultOptions.maxArrayLength = null;
which will impact all calls toconsole.log
andutil.format
-
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))
?
Related videos on Youtube
![Anthony](https://i.stack.imgur.com/7HdsS.jpg?s=256&g=1)
Anthony
Updated on February 06, 2020Comments
-
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 over 7 yearsTry
console.log(JSON.stringify(arr, null, 1));
. -
jfriend00 over 7 yearsSo, 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 over 5 yearsIf 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 over 7 yearsHere is the support. developer.mozilla.org/en-US/docs/Web/API/Console/table
-
Anthony over 7 yearsDon't want a different package sorry, hoping to do this with native node
-
Anthony over 7 yearsI want to add these items to an existing array.
-
George almost 7 yearsif you want to add one array to another, why not use Array.concat?
-
Magnus Bodin over 6 yearsSpecifically I like also to do
console.log(util.inspect(arrayofObjects, {maxArrayLength: null, depth:null }))
to go deep into objects as well. -
Anthony almost 6 yearsJust had a chance to check this, saving as the proper answer
-
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 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 useconsole.dir
instead ofconsole.log
in order to get it to work. -
Michael Hellein over 5 yearsJust 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 over 5 yearssame here, the other option would print the same thing
-
nonopolarity about 3 yearsI 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 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). Henceconsole.dir()
from the top answer satisfies better the question.