JavaScriptFundamental (ES6), part 1
Omit a key with object rest in JavaScript
24 Object rest, and omitting a key
fairUse ... in an object pattern to make a copy of a record without one sensitive field. Then check what the rest object does and does not pick up: key order, inherited properties, non-enumerable ones, and nested objects.
Stuck? Three hints, smallest first
- 1.
const { pin, ...safe } = rownames the key you want gone and keeps the rest. - 2. The rest object takes own enumerable properties only — the same set
Object.keysreports. - 3. It is one level deep, like every other spread.
const parses = (src) => {
try { new Function(src); return 'parsed'; } catch (e) { return e.constructor.name; }
};
const row = { id: 9, name: 'Emeka', pin: '1234', area: 'Yaba' };
const { pin, ...safe } = row;
const { name: who, ...others } = row; // a renamed pick is still omitted
const nested = { id: 1, meta: { seen: true } };
const { id, ...restOfNested } = nested;
const proto = { inherited: 'from the prototype' };
const child = Object.create(proto);
child.own = 'own value';
const { ...ownOnly } = child;
const hidden = {};
Object.defineProperty(hidden, 'secret', { value: 'not enumerable', enumerable: false });
hidden.shown = 'enumerable';
const { ...visible } = hidden;
const notLast = parses('const { ...r, a } = {};');
console.log('without the pin :', safe);
console.log('key order kept :', Object.keys(safe));
console.log('source intact :', 'pin' in row);
console.log('renamed pick :', who, '->', Object.keys(others));
console.log('nested shared :', restOfNested.meta === nested.meta);
console.log('inherited :', ownOnly, '| non-enumerable:', visible);
console.log('rest not last :', notLast);
Output
8 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 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('rest gives back everything the pattern did not name', safe, { id: 9, name: 'Emeka', area: 'Yaba' });
test('in the order the source declared them', Object.keys(safe), ['id', 'name', 'area']);
test('the source is not modified', 'pin' in row, true);
test('a renamed pick is omitted from the rest as well', Object.keys(others), ['id', 'pin', 'area']);
test('the rest object is shallow, so a nested object is shared', restOfNested.meta === nested.meta, true);
test('inherited properties are not copied', ownOnly, { own: 'own value' });
test('nor are non-enumerable ones', visible, { shown: 'enumerable' });
test('the rest property must come last', notLast, 'SyntaxError');
const { password, ...safe } = user is the clean way to strip a field before logging or returning a record, and it is better than delete because the original is left alone. Note it binds password as a variable you probably do not want; some linters will flag it as unused, which is the price.
The set it copies is exactly Object.keys: own, enumerable, string-keyed. Anything on the prototype, anything hidden with enumerable: false, and any getter's definition (the value is read once and frozen into a plain property) does not survive the trip.