Object property assignment with destructuring?

ericsoco

I'd like to use ES6 destructuring to assign properties of an object, but can't figure out the syntax.

<= ES5:

var dst = {};  // already in existence, with its own props, methods, etc.
var src = { a: 'foo', b: 'bar', c: 'baz' };
dst.a = src.a;
dst.b = src.b;

>= ES6 (my own made-up, not-working syntax):

let dst = {};
let src = { a: 'foo', b: 'bar', c: 'baz' };
dst[{a, b}] = src;

Is it possible to use destructuring assignment onto an object? What's the correct syntax?

EDIT: In my use case, dst is an object that existed well before needing to merge a subset of src's properties; it is not a new Object created solely to 'borrow' from src.

Ry-

I think you’re going to have to repeat dst:

({a: dst.a, b: dst.b} = src);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

destructuring assignment of array stored in object property

Typescript Object destructuring results in "Property assignment expected."

When using object destructuring assignment, why is a property "name" cast to string?

Destructuring assignment vs object property access in ES6

Destructuring assignment in object creation

Selective object destructuring with assignment in JavaScript?

Nested in object array destructuring with assignment

Javascript object destructuring with assignment in Babel

Destructuring an object with "delete" as property

Destructuring for get the first property of an object?

Partial object copying using Destructuring Assignment

Destructuring assignment to costruct a new object - Is it possible?

Destructuring assignment - object properties to variables in C#

Use of Colon in object assignment destructuring Javascript

Destructuring assignment in function call while preserving the object

Object destructuring assignment with default value of itself

How to use a destructuring assignment inside an object

Array destructuring into object assignment in a single instruction

Object Destructuring Assignment Behaving Weirdly in Constructor

Property object assignment

JavaScript object property assignment

Accessing object property by assignment

Property does not exists on type when destructuring assignment and 'Pick' in Typescript

why property does not exist on type in object destructuring?

Omit property variable when using object destructuring

object destructuring: how to use intermediate nested property

Destructuring object having hyphen in property name

Object destructuring with property names that are not valid variable names

How can I use this in the left side of the object destructuring assignment?