12

I have an object of objects and I want to iterate through the objects and show them in the browser; I have the following code, but it just shows me [object Object][object Object][object Object]; how can I show the actual objects?

my my_obj looks like:

{
    "User": {
        "description": "A user."
    },
    "Media": {
        "description": "A media ."
    }
}

var output = "";
for (var key in my_obj) {
    output += my_obj[key];
}
response.send(output);

Thanks

4
  • 1
    try console.log and you can see what the object is in the browser console. Commented Jul 30, 2014 at 16:26
  • even in console shows [object Object][object Object][object Object] Commented Jul 30, 2014 at 16:28
  • @ajp15243 thanks dude! If you wish put it as an answer and I will mark it as solved! Thanks again Commented Jul 30, 2014 at 16:30
  • try console.log in the developer console of your browser Commented Jul 30, 2014 at 16:30

2 Answers 2

19

It looks like this question is essentially a duplicate of yours.

The accepted answer for that question uses the console object to print the contents of the object to the JavaScript debugging console in the browser's developer tools (usually accessible with F12). You can typically interact with the object, expanding and collapsing its properties, in the logged output in the console.

console.log(my_obj);

However, this doesn't provide an easy way to print the contents of the object to the webpage. For that, you can follow the above-linked question's highest-voted answer, which uses the JSON object, specifically it's stringify method.

var my_obj_str = JSON.stringify(my_obj);

This method returns a stringified version of your object (i.e. it will appear like an object literal), which can then either be logged to the console like above (although it would be a string, not an interactive object!), or put into the webpage in whatever manner you like putting strings into webpage content.

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

Comments

2

You might need to use JSON.stringify() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Comments