3

Say I have defined an array of object like so:

 var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}, {firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}];

Now doing

console.log(person); 

returns "[Object,Object]"

I want to print the details of the array. How do I do this?

1
  • 1
    "Print" in what sense? Just to the console? What is the desired output format? [Object, Object] indicates an array containing two objects. In the console you should see a little arrow to the left of that, and if you click it it will expand the array to show more details of the contents. Commented Apr 17, 2017 at 8:07

6 Answers 6

4

Try this: console.table(person);

This would print the object in tabular format which would be easy on the eyes. Result would look like this: enter image description here

This is a lesser known feature compared to console.log but it is very handy at times.

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

1 Comment

Interesting. I didn't know you can do that! Though it's pretty buggy on Chrome (mac) and causing all sorts of rendering issues.
3
console.log(JSON.stringify(person));

That's one way to do it.

Comments

2

var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}, {firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}] ;

console.log(JSON.stringify(person));

//if you want to parse
for (var i = 0; i < person.length; i++) {
    var object = person[i];
    for (var property in object) {
        alert('item ' + i + ': ' + property + '=' + object[property]);
    }
    }

IS this what you want? If not please elaborate.

3 Comments

Hi Sachin, Can you please explain how the following part is working? : for (var property in object) { alert('item ' + i + ': ' + property + '=' + object[property]); 1. How is property becoming the object property (firstname) and object[property] becoming it's value (John)?
there is a foreach applied for the jsonArray person which is breaking it into objects. And code iterates through each object. Secondaly, the code is picking up each property of object inside alert one by one.
Thanks so much. but, two questions 1. how is "property" becoming (say) first name and "object[property]" it's value (say) John? from what I understand property is a declared variable and not a reserved word correct? 2. How will this code work with foreach?
1

As others already pointed out, you can use JSON.stringify. However, your JSON shows Objects which are collapsable, so you have the information there. If you want to prettify your output further, you may try pretty-js or underscore.js.

Comments

1

You can do this way:

var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}, 
{firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}];

JSON.stringify(person);

Comments

1

Try this:

const something = JSON.stringify([
    {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}, 
    {firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}
  ], null, " ");

document.querySelector("#result").textContent = something;

// or in the console
console.log(something);
<pre id="result"></pre>

Comments

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.