Proposal: deleting properties in object initializer expression

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Proposal: deleting properties in object initializer expression

Douglas Meyer
With spread properties, it would be great to be able to remove specific properties:
```js
const obj1 = { one: 1, two: 2, three: 3 };
const obj2 = {
...obj1,
delete three // <-- new syntax
};
```

`obj2` would be `{ one: 1, two: 2 }`. This syntax is visually similar to the getter/setter syntax. This should also support the computed property approach:
```js
const propName="three";
const obj1 = { one: 1, two: 2, [propName]: 3 };
const obj2 = { ...obj1, delete [propName] };
```

There are 2 ways of currently doing this:
```js
const { three, ...obj2 } = obj1; // has the side-effect of creating unnecessary variable `three`.
delete obj2.three; // seems odd to create an object to then reach-in and remove things.
```
It is worth noting that `const obj2 = { ...obj1, three: undefined }` is not the same thing, as `obj2.hasOwnProperty('three')` would still return true.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss