The Object.getOwnPropertyNames()
static method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.
The Object.getOwnPropertyNames()
static method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.
Object.getOwnPropertyNames(obj)
obj
The object whose enumerable and non-enumerable properties are to be returned.
An array of strings that corresponds to the properties found directly in the given object.
Object.getOwnPropertyNames()
returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly in a given object obj
. The ordering of the enumerable properties in the array is consistent with the ordering exposed by a for...in
loop (or by Object.keys()
) over the properties of the object. The non-negative integer keys of the object (both enumerable and non-enumerable) are added in ascending order to the array first, followed by the string keys in the order of insertion.
In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError
. In ES2015, a non-object argument will be coerced to an object.
Object.getOwnPropertyNames("foo"); // TypeError: "foo" is not an object (ES5 code) Object.getOwnPropertyNames("foo"); // ["0", "1", "2", "length"] (ES2015 code)
const arr = ["a", "b", "c"]; console.log(Object.getOwnPropertyNames(arr).sort()); // ["0", "1", "2", "length"] // Array-like object const obj = { 0: "a", 1: "b", 2: "c" }; console.log(Object.getOwnPropertyNames(obj).sort()); // ["0", "1", "2"] Object.getOwnPropertyNames(obj).forEach((val, idx, array) => { console.log(`${val} -> ${obj[val]}`); }); // 0 -> a // 1 -> b // 2 -> c // non-enumerable property const myObj = Object.create( {}, { getFoo: { value() { return this.foo; }, enumerable: false, }, }, ); myObj.foo = 1; console.log(Object.getOwnPropertyNames(myObj).sort()); // ["foo", "getFoo"]
If you want only the enumerable properties, see Object.keys()
or use a for...in
loop (note that this will also return enumerable properties found along the prototype chain for the object unless the latter is filtered with Object.hasOwn()
).
Items on the prototype chain are not listed:
function ParentClass() {} ParentClass.prototype.inheritedMethod = function () {}; function ChildClass() { this.prop = 5; this.method = function () {}; } ChildClass.prototype = new ParentClass(); ChildClass.prototype.prototypeMethod = function () {}; console.log(Object.getOwnPropertyNames(new ChildClass())); // ["prop", "method"]
This uses the Array.prototype.filter()
function to remove the enumerable keys (obtained with Object.keys()
) from a list of all keys (obtained with Object.getOwnPropertyNames()
) thus giving only the non-enumerable keys as output.
const target = myObject; const enumAndNonenum = Object.getOwnPropertyNames(target); const enumOnly = new Set(Object.keys(target)); const nonenumOnly = enumAndNonenum.filter((key) => !enumOnly.has(key)); console.log(nonenumOnly);
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
getOwnPropertyNames |
5 | 12 | 4 | 12 | 5 | 18 | 4 | 12 | 5 | 1.0 | 4.4 | 1.0 | 0.10.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames