Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Deleting keys in object spread #53

Open
Deco opened this issue Aug 16, 2017 · 2 comments
Open

Deleting keys in object spread #53

Deco opened this issue Aug 16, 2017 · 2 comments

Comments

@Deco
Copy link

Deco commented Aug 16, 2017

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
Copy link
Member

hax commented Sep 1, 2017

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
Copy link

taion commented Jan 14, 2018

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants