The output of the entire array: console.log and console.dir "... NUM more items]" are displayed

I am trying to register a long array to copy it in my terminal. However, if I try and register an array, it looks like this:

['item', 'item', >>more items<<< ... 399 more items ] 

How can I register an entire array to copy it very quickly?

+21
javascript arrays v8 dump
source share
8 answers

Setting maxArrayLength

There are several methods, each of which requires setting maxArrayLength which defaults to maxArrayLength 100.

  1. Provide an override as an option for console.log or console.dir

     console.log( myArry, {'maxArrayLength': null} ); 
  2. Set util.inspect.defaultOptions.maxArrayLength = null; which will affect 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 })) 
+33
source share

What happened to myArray.forEach(item => console.log(item)) ?

+3
source share

Using console.table

Available in Node v10 + and in all modern web browsers , you can instead use console.table() , which will display a beautiful utf8 table, where each row represents an array element.

 > console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']); β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β” β”‚ (index) β”‚ a β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€ β”‚ 0 β”‚ 1 β”‚ β”‚ 1 β”‚ 'Z' β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜ 
+2
source share

Just found that the maxArrayLength option maxArrayLength well with console.dir :

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

+2
source share

If you only need elements:

 for(i = 0; i < array.length; i++) console.log(array[i]); 

If you want to also copy brackets and commas:

 console.log("["); var i; // inportant to be outside for(i = 0; i < array.length - 1; i++) console.log(array[i] + ","); console.log(array[i] + "]"); 
+1
source share

Here is the solution I tested in Chrome and Firefox:

 var a = Array(400).fill("'item'"); console.log('[' + a + ']'); 
0
source share

Michael Hellain’s answer didn’t work for me, but the close version worked

 console.dir(myArray, {'maxArrayLength': null}) 

This was the only solution that worked for me, since JSON.stringify() was too ugly for my needs, and I didn't need to write code to print it one at a time.

0
source share

Terrible, but this will work:

 function createFullArrayString(array){ let string = '['; for(const item of array){ string += `'${item}',\n` } string = string.substring(0, string.length - 2); string += ']'; return string } console.log(createFullArrayString(array)) 

The desire was to do this in the native node.

-2
source share

All Articles