Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node console.log() output array in one line

I use node v10.6.0.

Here's my codes:

console.log([{a:1, b:2}, {a:1, b:2}, {a:1, b:2}])
console.log([{a:1, b:2}, {a:1, b:2}, {a:1, b:2}, {a:1, b:2}, {a:1, b:2}, {a:1, b:2}, {a:1, b:2}, {a:1, b:2}, {a:1, b:2}])

the output is as following:

[ { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 } ]
[ { a: 1, b: 2 },
  { a: 1, b: 2 },
  { a: 1, b: 2 },
  { a: 1, b: 2 },
  { a: 1, b: 2 },
  { a: 1, b: 2 },
  { a: 1, b: 2 },
  { a: 1, b: 2 },
  { a: 1, b: 2 } ]

How can I make the second array output in one line, instead of spreading to multiple lines.

like image 961
mCY Avatar asked Jul 28 '18 16:07

mCY


Video Answer


2 Answers

I suggest using the following:

console.log(util.inspect(array, {breakLength: Infinity}))

Plus, util.inspect has a bunch of extra options to format and limit the output:

https://nodejs.org/api/util.html#utilinspectobject-options

like image 96
ericfossas Avatar answered Oct 31 '22 01:10

ericfossas


Although the output is not exactly the same as if console.log is used, it's possible to use JSON.stringify to convert the array to a string, then print it:

console.log(JSON.stringify(array))

Try it online!

It cannot process circular structures, however.

like image 29
user202729 Avatar answered Oct 31 '22 01:10

user202729