Skip to content
  • Object Static Methods · JavaScript

Object Static Methods

LearnViaNotes @learnvianotes· 2 min read

The Object constructor carries the toolbox for inspecting, copying, converting and grouping objects.

MethodDoes
Object.keys / values / entries(o)Own enumerable string keys / values / [key, value] pairs
Object.fromEntries(iterable)Pairs → object (inverse of entries)
Object.assign(target, ...src)Copy own enumerable props into target (mutates, shallow)
Object.create(proto, descriptors?)New object with the given prototype
Object.getPrototypeOf / setPrototypeOfRead / change the prototype
Object.defineProperty / definePropertiesAdd props with explicit attributes
Object.freeze / seal / preventExtensionsRestrict changes
Object.hasOwn(o, k)Own-property check (ES2022)
Object.is(a, b)SameValue equality
Object.groupBy(items, fn)Group into a null-prototype object (ES2024)
Object.getOwnPropertyNames / getOwnPropertySymbolsInclude non-enumerables / symbols

entries ⇄ fromEntries: transform an object

JavaScript
const prices = { apple: 1.2, bread: 2.5, milk: 0.99 };
const doubled = Object.fromEntries(
  Object.entries(prices).map(([k, v]) => [k, v * 2])
);
console.log(doubled);
const cheap = Object.fromEntries(Object.entries(prices).filter(([, v]) => v < 2));
console.log(cheap);
console.log(Object.fromEntries(new Map([["x", 1]])));
console.log(Object.fromEntries(new URLSearchParams("q=js&page=2")));
Output
{ apple: 2.4, bread: 5, milk: 1.98 }
{ apple: 1.2, milk: 0.99 }
{ x: 1 }
{ q: 'js', page: '2' }

assign vs spread

JavaScript
const defaults = { theme: "light", lang: "en" };
const user = { lang: "hi" };
const merged = Object.assign({}, defaults, user);
const spread = { ...defaults, ...user };
console.log(merged, spread);
Object.assign(defaults, { theme: "dark" });   // mutates the target
console.log(defaults);
Output
{ theme: 'light', lang: 'hi' } { theme: 'light', lang: 'hi' }
{ theme: 'dark', lang: 'en' }

Object.groupBy (ES2024)

JavaScript
const inventory = [
  { name: "asparagus", type: "veg", qty: 5 },
  { name: "banana", type: "fruit", qty: 0 },
  { name: "goat", type: "meat", qty: 23 },
  { name: "cherry", type: "fruit", qty: 5 },
];
const byType = Object.groupBy(inventory, ({ type }) => type);
console.log(Object.keys(byType), byType.fruit.length);
const stock = Map.groupBy(inventory, ({ qty }) => (qty > 0 ? "ok" : "restock"));
console.log(stock.get("restock"));
Output
[ 'veg', 'fruit', 'meat' ] 2
[ { name: 'banana', type: 'fruit', qty: 0 } ]

Object.create

JavaScript
const animal = { speak() { return `${this.name} makes a sound`; } };
const dog = Object.create(animal);
dog.name = "Rex";
console.log(dog.speak(), Object.getPrototypeOf(dog) === animal);
const dict = Object.create(null);          // no prototype at all
dict.key = "v";
console.log("toString" in dict, dict);
Output
Rex makes a sound true
false [Object: null prototype] { key: 'v' }

Object.keys vs Object.getOwnPropertyNames vs Reflect.ownKeys?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report