← Back to JavaScript, page 4

javascript-0062 · Easy fresher

Which Object methods do you use to read, copy, and transform plain objects, and how does property order work?

Object.keys, values, and entries list own enumerable properties, Object.fromEntries builds an object back from pairs, Object.assign and spread copy properties, and Object.hasOwn checks ownership. Property order follows a fixed rule: integer-like keys ascending first, then string keys in creation order. Together these methods allow filtering and mapping objects the way arrays are handled.

Full answer

An object is a collection of key-value pairs. The static methods on Object read and reshape it.

Object.keys(obj), Object.values(obj), and Object.entries(obj) return arrays of the own, enumerable, string-keyed properties. Object.entries gives [key, value] pairs, which can be passed to array methods, and Object.fromEntries(pairs) turns pairs back into an object. This pair is the standard way to map or filter an object: convert to entries, use map or filter, convert back.

Object.assign(target, ...sources) copies properties into target and returns it, so it mutates the first argument. Spread, { ...a, ...b }, creates a new object instead, and later keys win. Both are shallow and read getters as plain values.

Object.hasOwn(obj, key) tells whether the key belongs to the object itself, and it replaced obj.hasOwnProperty(key), which can fail for objects without a prototype. The in operator also checks inherited keys. delete obj.key removes a property.

Property order is predictable. Keys that look like array indexes ('1', '42') come first in ascending numeric order, then other string keys in the order they were added, then symbols. This is why an object with keys 'b', '2', 'a', '1' lists 1, 2, b, a.

Shorthand properties ({ name }), computed keys ({ [key]: value }), and method shorthand keep object literals short. For dictionaries with arbitrary keys, a Map is often the better structure.

javascript
const marks = { maths: 90, physics: 72, chemistry: 35 };

console.log(Object.keys(marks), Object.values(marks));
console.log(Object.entries(marks));

const passed = Object.fromEntries(Object.entries(marks).filter(([, score]) => score >= 40));
console.log(passed);
const curved = Object.fromEntries(Object.entries(marks).map(([subject, score]) => [subject, score + 5]));
console.log(curved);

const total = Object.values(marks).reduce((sum, n) => sum + n, 0);
console.log(total);

const defaults = { theme: 'light', pageSize: 10 };
const target = { pageSize: 25 };
const result = Object.assign(target, defaults);
console.log(result === target, target);
console.log({ ...defaults, pageSize: 50 }, defaults);

const key = 'dynamic';
const name = 'Asha';
console.log({ name, [key + 'Key']: true, greet() { return 'hi'; } });

const mixed = { b: 1, 2: 'two', a: 2, 1: 'one', [Symbol('s')]: 'symbol' };
console.log(Object.keys(mixed), Reflect.ownKeys(mixed));

const inherited = Object.create({ fromPrototype: true });
inherited.own = true;
console.log('fromPrototype' in inherited, Object.hasOwn(inherited, 'fromPrototype'), Object.keys(inherited));

const bare = Object.create(null);
bare.x = 1;
console.log(Object.hasOwn(bare, 'x'), typeof bare.hasOwnProperty);

delete marks.chemistry;
console.log(marks, Object.entries(marks).length);

Run with Node.js v24.22.1 as js-62.js; two runs gave identical output. Output: [ 'maths', 'physics', 'chemistry' ] [ 90, 72, 35 ] [ [ 'maths', 90 ], [ 'physics', 72 ], [ 'chemistry', 35 ] ] { maths: 90, physics: 72 } { maths: 95, physics: 77, chemistry: 40 } 197 true { pageSize: 10, theme: 'light' } { theme: 'light', pageSize: 50 } { theme: 'light', pageSize: 10 } { name: 'Asha', dynamicKey: true, greet: [Function: greet] } [ '1', '2', 'b', 'a' ] [ '1', '2', 'b', 'a', Symbol(s) ] true false [ 'own' ] true undefined { maths: 90, physics: 72 } 2

Key points

  • keys, values, and entries list own enumerable string-keyed properties.
  • fromEntries is the inverse of entries; together they map and filter objects.
  • assign mutates its target; spread creates a new object; both are shallow.
  • hasOwn checks own keys; in also checks inherited keys.
  • Order: integer-like keys ascending, then string keys by creation, then symbols.

Common mistakes

  • Using Object.assign with a shared object as the first argument and mutating it by accident.
  • Assuming keys are listed in insertion order when some keys are integer-like.
  • Calling obj.hasOwnProperty on an object with a null prototype.
  • Expecting Object.keys to include inherited properties or symbols.

Follow-ups

  • What is the difference between Object.entries and Map entries?
  • How do you pick or omit certain keys from an object?
  • How would you merge two objects deeply?

Tips

  • Show the entries, map or filter, fromEntries round trip; it is the most useful idiom.
  • State the key-order rule with the integer-like example.
  • Prefer Object.hasOwn over hasOwnProperty in new code and say why.