11374

node console.log() output array in one line

<h3>Question</h3>

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.


<h3>Answer1:</h3>

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:

<pre class="lang-javascript prettyprint-override">console.log(JSON.stringify(array))

Try it online!

It cannot process circular structures, however.


<h3>Answer2:</h3>

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#util_util_inspect_object_options

来源:https://stackoverflow.com/questions/51573010/node-console-log-output-array-in-one-line

Recommend

  • node console.log() output array in one line
  • node console.log() output array in one line
  • Perl: write speed mystery?
  • CSS slide bottom border on click
  • How to integrate application with excel macros?
  • netty 4 performance: multi channels or only one?
  • How to do multi-class image classification in keras?
  • How to install PHP pthreads in cpanel?
  • Is there a way to convert all existing table data to UTF8 collation?
  • Customize jQuery submenu positioning
  • Is it possible to add a hyperlink to a UIAlertController?
  • Limit number of button clicks
  • Vue.js 2: Vue cannot find files from /assets folder (v-for)
  • Sorting Custom Listview Items Using Spinner Android
  • Jekyll - How do I create pages in the root directory?
  • Extract table from Powerpoint
  • MFMailComposer send email without presenting view
  • $this->a->b->c->d calling methods from a superclass in php
  • dmtracedump doesn't work, HELP!
  • Facebook friend list in Facebook Android SDK 3.14
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • Find all parks for a given zipcode with google maps
  • xpath assertion failure with dynamic xpath
  • Annotate objects in a queryset with next and previous object ids
  • how to run a different select statement based on condition in Hive SQL
  • How to define something in JavaScript [closed]
  • How to restrict use of third party camera app from your app
  • Typeahead.js does give me suggestions but doesn't select them
  • Neo4j…how to get a visual representation of my data?
  • VSTS work items list through REST API
  • How to decleare char *const argv[] in swift [duplicate]
  • Bitrate JWplayer
  • Grails - How to implement a foreign key relationship not using an id column?
  • Accessing Arguments, Workflow Variables from custom activities
  • Firebase: How to read from external DB?
  • convert json to excel in java
  • ReferenceError: TextEncoder is not defined
  • XSLT Transformation to validate rules in XML document