简体   繁体   中英

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: ""}

在此处输入图片说明

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

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.

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()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM