I. Ahmed
I. Ahmed

Reputation: 2534

How to get the first portion of object printed by console.log

I have an error object, I have printed it using the code: console.log(error);

The console printed value is:

SigninError {id: "el-20003", description: ""}

enter image description here

I need to get the "SigninError" as string. How to get that?

Upvotes: 0

Views: 49

Answers (2)

Chirag Ravindra
Chirag Ravindra

Reputation: 4830

That string is probably the class name of the object. You can get it like so:

console.log(error.constructor.name)

Note: This may or may not work for you depending on how the Error object class was instantiated and whether or not you use a destructive minifier or a code mangler.

Upvotes: 2

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Use Object.keys(error) to get the keys of error object as an array and then use toString() to change it to string value:

let error = {
 SigninError:{id: "el-20003", description: ""}
}
var keys = Object.keys(error);
console.log(keys.toString());

Upvotes: 1

Related Questions