Reputation:
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.
Upvotes: 1
Views: 3364
Reputation: 31
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);
Upvotes: 1
Reputation: 94
use console.log(JSON.stringify(obj))
to display the proper json string of the object.
Upvotes: 0
Reputation:
So the correct answer is using console.log(Object.getOwnPropertyNames(intended_obj)); This will list all the properties of the object in an array!
Upvotes: 0