@rbxts/jsnatives



Object.keys

Returns an array of a given object's own enumerable property names.

Signature

function keys<T>(obj: ReadonlyArray<T>): Array<number>
function keys<T>(obj: ReadonlySet<T>): Array<T>
function keys<T>(obj: ReadonlyMap<T, unknown>): Array<T>
function keys<T extends object>(obj: T): Array<keyof T>

Description

The Object.keys() method returns an array of a given object's own enumerable property names. For different types of objects, it returns different types of keys:

Examples

Object keys

const obj = {a: 1, b: 2, c: 3};
print(Object.keys(obj)); // ["a", "b", "c"]

Proxy keys

const proxiedObj = new Proxy({}, {
ownKeys: () => {
return ["a", "b", "c"];
},
get: (target, prop) => {
print("key trap");
}
});
print(Object.keys(proxiedObj)); // ["a", "b", "c"] (no "key trap" logged)

Set keys

const set = new Set([1, 2, 3]);
print(Object.keys(set)); // [1, 2, 3]

Map keys

const map = new Map([["a", 1], ["b", 2], ["c", 3]]);
print(Object.keys(map)); // ["a", "b", "c"]

Array keys

const array = [1, 2, 3];
print(Object.keys(array)); // [0, 1, 2]