1

I have an object, say object obj. Obj has several properties(int id, boolean status, etc) and I know I want to access some of the properties. Yet because I do not know where I defined this object, I would like to know the best way to display all the properties of the object using console.log.

Hey all, when I tried using console.log(obj); it did not work. That is the reason I posted this question. For some reason, in my application; it returned obj obj instead of the properties or letting me open up the properties. I haven't tried the console.log(JSON.stringify(obj)); but the command console.log(Object.getOwnPropertyNames(intended_obj));worked exactly as intended.

  • just `console.log(obj)`. any modern browser will print it in a nice way so you can navigate the properties. nb : console.log has nothing to do with angular, this is just Javascript – Pac0 Apr 13 '18 at 23:02
  • When I ran that, it would come back as obj obj. That's why I thought this may have been an Angular JS thing; and that's why the question was posted. –  Apr 13 '18 at 23:07
  • can you create a plunker or stackblitz demonstrating this ? Also, which browser are you using ? – Pac0 Apr 13 '18 at 23:07
  • I was using Google Chrome and also I already found the answer and posted it below. –  Apr 13 '18 at 23:09
  • Also, thanks for your help as well –  Apr 13 '18 at 23:09
  • Possible duplicate of [How to show full object in Chrome console?](https://stackoverflow.com/questions/4482950/how-to-show-full-object-in-chrome-console) – davidkonrad Apr 14 '18 at 08:56
  • You are showing object as a String data type. Pass it as Object type. If you have data as String than parse it with JSON.parse() – Burak Akyıldız Apr 14 '18 at 09:13
  • Use `console.dir(obj)` ... – davidkonrad Apr 14 '18 at 21:01

3 Answers3

1

You could also use console.table(obj). According to MDN:

// an object whose properties are strings
function Person(firstName, lastName){
  this.firstName = firstName;
  this.lastName = lastName;
}

var me = new Person("John", "Smith");
console.table(me);

Browser's console: image

GiorgosPap
  • 31
  • 1
  • 7
0

So the correct answer is using console.log(Object.getOwnPropertyNames(intended_obj)); This will list all the properties of the object in an array!

0

use console.log(JSON.stringify(obj)) to display the proper json string of the object.

Amit Shahani
  • 94
  • 3
  • 12