Creating new object during destruction without duplicate code

When we create new object using existing one we use destruction to get properties and assign them to the new object

const {id, name} = user;
return {id, name};

Wouldn't it be nice if just take out the new object during destruction instead of assign them with a keyword like out

const {id, name} out viewModel = user;
return viewModel;

or shortly

return {id, name} out viewModel = user;

or even better if there is no declaration keyword

return {id, name} = user;

Sounds like you want a pick.

Note: link above largely discusses Object API and not so much syntax but on various occasions a syntax has also been proposed, ex: dotstructuring.

1 Like

By the way, this is already valid syntax. It will assign and to existing id and name variable in scope, then return user. In sloppy-mode it will create id and name as globals if they don't exist, in strict mode it will throw.

yeah it is valid syntax but it returns whole object and assignment works only in scope if they are not defined global scope. But I think it would be nice to get filtered keys with destruction as a new object, not the object on the right