Getting JavaScript object key list

后端 未结 17 1138
野性不改
野性不改 2020-11-22 01:27

I have a JavaScript object like

var obj = {
   key1: \'value1\',
   key2: \'value2\',
   key3: \'value3\',
   key4: \'value4\'
}

How can I

17条回答
  •  情歌与酒
    2020-11-22 02:09

    In JavaScript, an object is a standalone entity, with properties and type.

    For fetching values from Object in form of array: Object.values(obj) // obj is object name that you used Result -> ["value1", "value2", "value3", "value4"]

    For fetching keys from Object in form of array: Object.keys(obj) // obj is object name that you used Result -> ["key1", "key2", "key3", "key4"]

    As both functions are returning array you can get the length of keys or value by using length property. For instance - Object.values(obj).length or Object.keys(obj).length

提交回复
热议问题