11

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.

5
  • That's just how Node prints long objects... Commented Jul 28, 2018 at 16:02
  • 1
    @user202729, thanks for your remind. JSON.stringify solved my problem nicely! Commented Jul 28, 2018 at 16:14
  • util.inspect seems a little weird. As util.inspect(...a long array...) will output '[ { a: 1, b: 2 },\n { a: 1, b: 2 },\n { a: 1, b: 2 },\n .... Why include \n ... Commented Jul 28, 2018 at 16:16
  • 1
    @mCY There are parameters to control the output format for util.inspect. (read the documentation) Commented Jul 28, 2018 at 16:18
  • @user202729 got it : ) Commented Jul 28, 2018 at 16:25

3 Answers 3

16

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.

Sign up to request clarification or add additional context in comments.

Comments

8

Why not using console.table instead?

This give this nice table: https://tio.run/##y0osSyxOLsosKNHNy09J/Z9YVJRYaRtdnWhlqKOQZGVUq6NAW3bs/@T8vOL8nFS9ksSknFQNsAM0//8HAA

┌─────────┬───┬───┐
│ (index) │ a │ b │
├─────────┼───┼───┤
│    0    │ 1 │ 2 │
│    1    │ 1 │ 2 │
│    2    │ 1 │ 2 │
│    3    │ 1 │ 2 │
│    4    │ 1 │ 2 │
│    5    │ 1 │ 2 │
│    6    │ 1 │ 2 │
│    7    │ 1 │ 2 │
│    8    │ 1 │ 2 │
└─────────┴───┴───┘

Comments

7

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

3 Comments

There is another option color: true to keep the colors.
I get this: C:\abc.js:91 util.inspect(found, {breakLength: Infinity} ) ^ ReferenceError: util is not defined at Object.<anonymous> (C:\abc.js:91:2) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 [Finished in 0.1s]
@agiopnl you need to import it ==> const util = require('util');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.