@rbxts/jsnatives
Object.fromEntries
Transforms a list of key-value pairs into an object.
Signature
function fromEntries<P extends readonly [string | number | symbol, unknown]>( i: ReadonlyArray<P>,) => Reconstruct< UnionToIntersection< P extends unknown ? { [k in P[0]]: P[1]; } : never >>
Description
The Object.fromEntries()
method transforms a list of key-value pair arrays into an object. This is the reverse operation of Object.entries()
.
The method takes an iterable of key-value pairs and returns a new object whose properties are given by those entries.
Examples
Basic usage
const entries = [["a", 1], ["b", 2], ["c", 3]];const obj = Object.fromEntries(entries);
print(obj); // {a: 1, b: 2, c: 3}
Creating objects with complex values
const nestedEntries = [ ["user", {name: "Alice", role: "Admin"}], ["settings", {theme: "dark", notifications: true}], ["stats", [10, 20, 30]]];
const complexObj = Object.fromEntries(nestedEntries);print(complexObj.user.name); // "Alice"print(complexObj.settings.theme); // "dark"print(complexObj.stats[1]); // 20
Type inference
// TypeScript can infer the resulting object typeconst typedEntries = [ ["id", 1], ["name", "Product"], ["inStock", true]] as const;
const product = Object.fromEntries(typedEntries);// product has type { id: number; name: string; inStock: boolean }