Skip to main content
CodeOath
← All posts

JavaScript60 min total · 16 parts

JavaScript Array and Object Methods Cheat Sheet

Part 7 of 16 · ~3 min

Object Methods

Zoom into a single record — an interview scorecard for one candidate:

const feedback = { communication: 8, systemDesign: 7, coding: 9 };
MethodDoesTry it on feedback
Object.keys(obj)just the key names, as a plain arrayObject.keys(feedback) → ["communication", "systemDesign", "coding"]
Object.values(obj)just the values, lined up in that same orderObject.values(feedback) → [8, 7, 9]
Object.entries(obj)every key zipped together with its valueObject.entries(feedback) → [["communication", 8], ...]
Object.fromEntries(entries)runs .entries() backwards, pairs back into an objectObject.fromEntries([["communication", 8]]) → { communication: 8 }
Object.assign(target, ...sources)layers each source's fields onto target, changing target itselfObject.assign({}, feedback, { coding: 10 })
Object.freeze(obj)locks the object's own top-level fields against being set — nothing nested is protectedObject.freeze(feedback).coding = 1 — silently fails
Object.isFrozen(obj)tells you whether .freeze() already ran on this objectObject.isFrozen(Object.freeze({})) → true
Object.hasOwn(obj, key)true only when key belongs to obj directly, not something it inheritedObject.hasOwn(feedback, "coding") → true

Since one is the exact reverse of the other, Object.entries() and Object.fromEntries() tend to show up together any time the job is "touch every value, end up with an object again" — sandwich a .map() between them instead of writing the loop by hand. Turning raw 0–10 scorecard ratings into percentages, for example:

const asPercent = Object.fromEntries(
  Object.entries(feedback).map(([category, rating]) => [category, rating * 10])
);
// { communication: 80, systemDesign: 70, coding: 90 }

Object.assign() is how you'd layer role-specific weighting on top of a shared default — say, system design counts double for a senior opening:

const defaultWeights = { communication: 1, systemDesign: 1, coding: 1 };
const seniorWeights = Object.assign({}, defaultWeights, { systemDesign: 2 });
// { communication: 1, systemDesign: 2, coding: 1 }

Note the empty {} as the first argument — Object.assign() mutates whatever you pass as the target, so merging into a fresh empty object rather than defaultWeights itself is what keeps the original default untouched. Skip that and you've silently rewritten your shared defaults for every future candidate.

Object.freeze() locks a finalized scorecard once an interviewer submits it — no more edits, accidental or otherwise:

const finalFeedback = Object.freeze({ communication: 8, systemDesign: 7, coding: 9 });
finalFeedback.coding = 2;          // silently fails outside strict mode, throws inside a module
Object.isFrozen(finalFeedback);    // true

But .freeze() is shallow, and this is the part that catches people: locking an object down protects only its own top level, not whatever lives one step further in.

const candidateRecord = Object.freeze({ name: "Priya Shah", feedback: { coding: 9 } });
candidateRecord.feedback.coding = 2; // succeeds! freeze() only locked the top level, not `feedback`

Object.hasOwn() is a newer, safer replacement for the old obj.hasOwnProperty(key) pattern — same question, "does this object have this key on itself, not inherited from somewhere up the prototype chain," but it works even on an object that has no .hasOwnProperty method to call (one created with Object.create(null), for instance), and it can't be fooled by a candidate record that happens to have its own property literally named hasOwnProperty.

The newer shortcut for that byRole grouping

The .reduce() pattern from the last section — building { backend: [...], frontend: [...] } by hand — has a direct built-in now:

const byRole = Object.groupBy(candidates, c => c.role);
// { backend: [Priya, Amara, Lena], frontend: [Diego, Sam] }

Same result, no manual accumulator or ??= needed. Two things worth knowing before you reach for it: it needs a modern runtime (it landed in 2024), and the object it returns has no prototype — so byRole.hasOwnProperty doesn't exist on it, but Object.hasOwn(byRole, "backend") still works fine, which is one more reason that method exists.