node console.log() output array in one line

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 │
└─────────┴───┴───┘

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.


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