tc39 / proposal-object-rest-spread

Rest/Spread Properties for ECMAScript
1.49k stars 85 forks source link

Deleting keys in object spread #53

Open Deco opened 7 years ago

Deco commented 7 years ago

Has there been any discussion about removing/filtering keys during spread?

For example:

let x = { a: 1, b: 2, c: 3, };
let y = { ...x, c: undefined, };
// y is { a: 1, b: 2, c: undefined, }
let z = { ...x, delete c, }; // possible syntax
// z is { a: 1, b: 2, }

At the moment, z is currently achievable using this multi-statement code:

let z = { ...x, };
delete z.c;

or using this single-expression but rather verbose and potentially-less-optimizable code:

let z = Object.entries(x).reduce((o, [k, v]) => (k !== 'c' && (o[k] = v), o), {})

It'd be nice if it could be done in an object spread expression, perhaps using my proposed syntax.

hax commented 7 years ago

See https://github.com/tc39/proposal-object-rest-spread/blob/master/Issues.md#linters-complain-about-unused-variables

But _ as an special name to indicate unused var is just a workaround. I think we may reuse some keywords/symbols here.

var {x: void, ...rest} = obj

Or

var {x: delete, ...rest} = obj

Or

var {x!, ...rest} = obj
taion commented 6 years ago

It would be really nice if there were a way to remove properties when using a rest property without creating a binding.