Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print an object as JSON to console in Angular2?

I'm using a service to load my form data into an array in my angular2 app. The data is stored like this:

arr = []
arr.push({title:name})

When I do a console.log(arr), it is shown as Object. What I need is to see it as [ { 'title':name } ]. How can I achieve that?

like image 216
Bijith komalan Avatar asked Aug 19 '16 20:08

Bijith komalan


2 Answers

you may use below,

  JSON.stringify({ data: arr}, null, 4);

this will nicely format your data with indentation.

like image 112
Madhu Ranjan Avatar answered Nov 11 '22 09:11

Madhu Ranjan


To print out readable information. You can use console.table() which is much easier to read than JSON:

console.table(data);

This function takes one mandatory argument data, which must be an array or an object, and one additional optional parameter columns.

It logs data as a table. Each element in the array (or enumerable property if data is an object) will be a row in the table

Example: enter image description here

enter image description here

like image 26
Haifeng Zhang Avatar answered Nov 11 '22 11:11

Haifeng Zhang