简体   繁体   中英

How to print property name rather than the whole JS object?

Let's suppose :

this.state.keys[index] =
    Object { -ID01: Object, -ID02: Object, -ID03: Object, -IDO4: Object }

How to print -ID02 for example ? I am looking for the propriety name -ID02 not what's inside and its actual values

// Expected Output  -ID02 (string)
Object.keys(this.state.keys[index]) 

will give you the keys in that object as an array.

More info here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

you could Object.keys() and index into the key of interest. For eg -ID02 would be at index 1 . Therefore, Object.keys(this.state.keys)[1] would give you what you are looking for.

If you have an object obj , then Object.keys(obj) will return an array of all the keys in that object. For eg:

const obj = { '-ID01': Object, '-ID02': Object, '-ID03': Object, '-IDO4': Object };
let keys = Object.keys(obj); //['-ID01', '-ID02', '-ID03', '-ID04']

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