@rbxts/jsnatives



Object.entries

Returns an array of a given object's own enumerable property [key, value] pairs.

Signature

function entries<T>(obj: ReadonlyArray<T>): Array<[number, NonNullable<T>]>
function entries<T>(obj: ReadonlySet<T>): Array<[T, true]>
function entries<K, T>(obj: ReadonlyMap<K, T>): Array<[K, NonNullable<T>]>
function entries<T extends object>(obj: T): Array<[keyof T, NonNullable<T[keyof T]>]>

Description

The Object.entries() method returns an array containing all of the [key, value] pairs of a given object's own enumerable string properties. The return type varies based on the input object type:

Examples

Object entries

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

Proxy entries

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

Proxy with undefined values

const proxiedWithUndefined = new Proxy({}, {
ownKeys: () => {
return ["a", "b", "c"];
},
get: (target, prop) => {
print("key trap", prop);
return prop === "b" ? undefined : prop;
}
});
print(Object.entries(a)); // [["a", "a"], ["b"], ["c", "c"]] (null values are not included)

Set entries

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

Map entries

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

Array entries

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