Object.keys()

Get an Object's top-level property keys as an Array.

Signature

Object.keys(object: Any) => Array<String>

Description

Object.keys() returns an Array with the top-level property keys of a provided Object. The Array does not contain keys for nested properties.

Object.keys() does not change the original object.

Parameters

Parameter Type Required Description

object

Object

Yes

Object to extract keys from.

Return value

Type Description

Array of Strings

An array containing the keys of the Object as Strings.

Examples

Basic example

Object.keys({
  bananas: 300,
  limes: 200,
  lemons: 500
})
[
  "bananas",
  "limes",
  "lemons"
]

Nested Object properties

Object.keys() only extracts an Object’s top-level property keys. It doesn’t extract nested property keys.

Object.keys({
  bananas: 300,
  limes: 200,
  lemons: {
    organic: 500
  }
})
// The results don't includes the nested `lemons.organic`
// property key.
[
  "bananas",
  "limes",
  "lemons"
]

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!