JavaScriptFundamental (ES6), part 1
Object.entries and Object.fromEntries in JavaScript
37 keys, values, entries and back again
fairTotal a table of rents through Object.entries, transform it and rebuild it with Object.fromEntries, then invert it so the values become the keys. Note what happens to the key order when you do.
Stuck? Three hints, smallest first
- 1.
Object.entriesgives[key, value]pairs, so a destructuredfor...ofreads them cleanly. - 2.
Object.fromEntriesis the inverse and also accepts aMap. - 3. The inverted object has numeric-looking keys, which brings the ordering rules of the previous exercise into play.
const rents = { A1: 12000, A2: 9000, B1: 15000 };
const labels = [];
for (const [code, amount] of Object.entries(rents)) labels.push(`${code}=${amount}`);
const total = Object.entries(rents).reduce((sum, [, amount]) => sum + amount, 0);
const roundTrip = Object.fromEntries(Object.entries(rents));
const raised = Object.fromEntries(Object.entries(rents).map(([code, amount]) => [code, amount + 1000]));
const inverted = Object.fromEntries(Object.entries(rents).map(([code, amount]) => [amount, code]));
const fromMap = Object.fromEntries(new Map([['A1', 1]]));
const duplicated = Object.fromEntries([['a', 1], ['a', 2]]);
const proto = { inherited: 'skipped' };
const child = Object.create(proto);
child.own = 'listed';
console.log('entries :', Object.entries(rents));
console.log('destructured in a loop:', labels);
console.log('total :', total);
console.log('raised :', raised);
console.log('inverted :', inverted, '<- keys reordered numerically');
console.log('from a Map:', fromMap, '| duplicate key:', duplicated);
console.log('own only :', Object.keys(child));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('entries pairs every key with its value', Object.entries(rents), [['A1', 12000], ['A2', 9000], ['B1', 15000]]);
test('destructuring the pair in a for-of reads well', labels, ['A1=12000', 'A2=9000', 'B1=15000']);
test('entries plus reduce totals an object', total, 36000);
test('fromEntries reverses entries exactly', roundTrip, rents);
test('mapping the pairs transforms the values', raised, { A1: 13000, A2: 10000, B1: 16000 });
test('swapping the pairs inverts the object', inverted[12000], 'A1');
test('but the new keys are integer-like, so they come back sorted', Object.keys(inverted), ['9000', '12000', '15000']);
test('fromEntries accepts a Map as well as an array', fromMap, { A1: 1 });
test('a repeated key keeps the last value', duplicated.a, 2);
Object.entries plus an array method plus Object.fromEntries is the standard way to map or filter an object, since objects have no such methods of their own. The whole transformation stays an expression, which keeps it assignable to a const.
Inverting is where the ordering rules ambush you. The values were amounts, so the inverted keys are numeric strings, so they come back sorted ascending rather than in the order the original object had them. If that matters, invert into a Map instead. All three of these functions see own enumerable string keys only — no prototype, no symbols — which is usually what you want and is worth knowing before you debug a missing key.