9

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?

1
  • 3
    try console.log(JSON.stringify(arr)) Commented Aug 19, 2016 at 20:25

6 Answers 6

14

you may use below,

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

this will nicely format your data with indentation.

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

2 Comments

@Bijithkomalan It's great that you thanked Madhu; if an answer worked for you please select and upvote
Nice, thanks. This is the kind of simple solution I expected to be able to find but didn't know where to look. Even better is just console.log(JSON.stringify(arr)) if you don't need the formatting and just want to see how the object might be passed to a REST endpoint or the like.
2

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

2 Comments

While this indeed does work, I don't see that it answers OP's question.
TIL console.table(x). looks nice!
0

first convert your JSON string to Object using .parse() method and then you can print it in console using console.table('parsed sring goes here').

e.g.

const data = JSON.parse(jsonString);
console.table(data);

Comments

0

Please try using the JSON Pipe operator in the HTML file. As the JSON info was needed only for debugging purposes, this method was suitable for me. Sample given below:

<p>{{arr | json}}</p>

Comments

-1

You could log each element of the array separately

arr.forEach(function(e){console.log(e)});

Since your array has just one element, this is the same as logging {'title':name}

1 Comment

Maximum effort for the same results you could achieve with JSON.stringify()
-2

you can print any object

console.log(this.anyObject);

when you write

console.log('any object' + this.anyObject);

this will print

any object [object Object]

1 Comment

This is kind of exactly what OP said they didn't want to do.

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.