JavaScript: Object.keys

By Xah Lee. Date: . Last updated: .

Object.keys is used to convert a Object to array.

Object.keys(obj)
Return a Array of string property keys that are own and Enumerable.
console.log(Object.keys({ "a": 3, "b": 4 }));
// [ "a", "b" ]
// true array example
console.log(Object.keys([3, 4, 5]));
// [ "0", "1", "2" ]

The following example shows that Symbol key and non-enumerable properties are ignored.

// create a object, such that only 1 property is both enumerable and string key
const xx = Object.create(Object.prototype, {
  "a": { value: 3, writable: true, enumerable: true, configurable: true },
  "b": { value: 3, writable: true, enumerable: false, configurable: true },
  [Symbol("x")]: {
    value: 3,
    writable: true,
    enumerable: true,
    configurable: true,
  },
});

const yy = Object.keys(xx);

console.log(yy.length === 1);
console.log(yy[0] === "a");
BUY
ΣJS
JavaScript in Depth

JS Obj Reference

Object Object

prototype