← Back to JavaScript, page 8

javascript-0160 · Easy fresher

How do property shorthand, computed property names, Object.entries, and Object.fromEntries work together?

Property shorthand writes { name } instead of { name: name }, method shorthand drops the function keyword, and computed keys write an expression in square brackets for the property name. Object.entries turns an object into an array of [key, value] pairs, and Object.fromEntries builds an object back from pairs, so together they let you transform objects with array methods such as map and filter.

Full answer

ES2015 added short ways to write objects.

Property shorthand: when a variable name matches the key, write { name, age } instead of { name: name, age: age }. Method shorthand: { greet() { ... } } instead of greet: function () { ... }. A shorthand method can use super, and it is not a constructor, but it still has its own this.

Computed property names: { [expression]: value } evaluates the expression and uses the result as the key. This is useful for dynamic keys ({ [field]: value }), for building keys from parts ([user_${id}]), for symbols ({ [Symbol.iterator]() {} }), and for updating state immutably: { ...state, [name]: value }. Computed keys can also be used in classes and for method names. If the computed value is not a string or symbol, it is converted to a string.

Object.entries(obj) returns an array of [key, value] pairs for the object's own enumerable string keys, in the standard order (integer-like keys ascending, then others in insertion order). Object.keys and Object.values return only keys or values. Object.fromEntries(iterable) does the reverse: it creates an object from any iterable of pairs, including a Map. The two together give a pattern for transforming objects: Object.fromEntries(Object.entries(obj).filter(...).map(...)). Typical tasks: pick or omit keys, map the values of an object, swap keys and values, convert between Map and object, and parse a query string into an object (Object.fromEntries(new URLSearchParams(search))).

Notes:

  • Duplicate keys in fromEntries: later pairs override earlier ones, and a repeated query string key keeps only the last value.
  • Symbols are ignored by entries.
  • Only own, enumerable properties are included. Inherited properties and non-enumerable ones are skipped.
  • A key like __proto__ in a literal sets the prototype, while ['__proto__'] creates a normal property.
javascript
const name = 'Asha';
const age = 30;
const person = { name, age, greet() { return `Hi, I am ${this.name}`; } };
console.log(person, person.greet());

const field = 'email';
const id = 7;
const key = Symbol('secret');
const dynamic = { [field]: 'a@example.com', [`user_${id}`]: true, [1 + 1]: 'two', [key]: 'hidden' };
console.log(dynamic, Object.keys(dynamic));

const state = { count: 1, theme: 'light' };
const update = (name, value) => ({ ...state, [name]: value });
console.log(update('theme', 'dark'), state);

const prices = { apple: 1.2, pear: 0.8, melon: 3.5 };
console.log(Object.entries(prices));
const doubled = Object.fromEntries(Object.entries(prices).map(([item, price]) => [item, price * 2]));
const cheap = Object.fromEntries(Object.entries(prices).filter(([, price]) => price < 2));
const swapped = Object.fromEntries(Object.entries({ a: 1, b: 2 }).map(([k, v]) => [v, k]));
console.log(doubled, cheap, swapped);

const pick = (object, keys) => Object.fromEntries(keys.filter((k) => k in object).map((k) => [k, object[k]]));
const omit = (object, keys) => Object.fromEntries(Object.entries(object).filter(([k]) => !keys.includes(k)));
console.log(pick(prices, ['apple', 'kiwi']), omit(prices, ['melon']));

const map = new Map([['x', 1], ['y', 2]]);
console.log(Object.fromEntries(map), new Map(Object.entries({ z: 3 })));
console.log(Object.fromEntries(new URLSearchParams('page=2&sort=asc&page=3')));

console.log(Object.entries({ b: 1, 2: 'two', a: 2, 1: 'one', [Symbol('s')]: 'skipped' }));
console.log(Object.fromEntries([['dup', 1], ['dup', 2]]));

const literal = { __proto__: { inherited: true } };
const computed = { ['__proto__']: 'plain property' };
console.log(literal.inherited, Object.keys(literal), Object.keys(computed));

Run with Node.js v24.22.1 as js-160.js; two runs gave identical output. Output: { name: 'Asha', age: 30, greet: [Function: greet] } Hi, I am Asha { '2': 'two', email: 'a@example.com', user_7: true, Symbol(secret): 'hidden' } [ '2', 'email', 'user_7' ] { count: 1, theme: 'dark' } { count: 1, theme: 'light' } [ [ 'apple', 1.2 ], [ 'pear', 0.8 ], [ 'melon', 3.5 ] ] { apple: 2.4, pear: 1.6, melon: 7 } { apple: 1.2, pear: 0.8 } { '1': 'a', '2': 'b' } { apple: 1.2 } { apple: 1.2, pear: 0.8 } { x: 1, y: 2 } Map(1) { 'z' => 3 } { page: '3', sort: 'asc' } [ [ '1', 'one' ], [ '2', 'two' ], [ 'b', 1 ], [ 'a', 2 ] ] { dup: 2 } true [] [ '__proto__' ]

Key points

  • Shorthand: { name } for matching variable names, and method shorthand without the function keyword.
  • Computed keys use [expression] to make dynamic, symbol, or generated keys.
  • Object.entries gives [key, value] pairs of own enumerable string keys; fromEntries builds an object from pairs.
  • entries plus array methods plus fromEntries transforms objects: pick, omit, map values, swap.
  • Later duplicate keys win; symbols and inherited keys are skipped.

Common mistakes

  • Writing {name: name} everywhere instead of shorthand, or using shorthand when the variable name differs from the key.
  • Forgetting the square brackets and getting the literal key name.
  • Expecting entries to include inherited properties or symbols.
  • Losing repeated values when turning a query string into an object.

Follow-ups

  • How would you invert an object whose values are not unique?
  • What is the order of keys that Object.entries returns?
  • When would you use a Map instead of an object for key-value data?

Tips

  • Show the entries, transform, fromEntries pattern with one example.
  • Mention the immutable update with a computed key.
  • Remember that later duplicates override earlier ones.