How to use ES6 destructuring assignment to assign one object to another

apple

I need the value of keys in object arrA to be copied from arrB based on key name. Here are my two objects:

let arrA = {
    'aaa':'',
    'bbb':'',
    'ccc':''
}
let arrb = {
    'aaa':'111',
    'bbb':'222',
    'ccc':'333',
    'ddd':'444',
    'eee':'555',
    ...
}

How do I do this with the ES6 deconstructive assignment:

arrA = {
    'aaa':'111',
    'bbb':'222',
    'ccc':'333'
}
Samathingamajig

Using destructing assignment, you'd have to explicitly define each property you'd want to copy:

let arra = {
  'aaa': '',
  'bbb': '',
  'ccc': ''
};
let arrb = {
  'aaa': '111',
  'bbb': '222',
  'ccc': '333',
  'ddd': '444',
  'eee': '555',
};

({aaa: arra.aaa, bbb: arra.bbb, ccc: arra.ccc} = arrb);

console.log(arra);

However, this code is very repetitive, and the worst part is that it's explicit with what gets copied.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use array destructuring and assign to an object property in JavaScript ES6

How to use destructuring assignment to define enumerations in ES6?

How to use a destructuring assignment inside an object

How to mix const and let when using object or array destructuring assignment in ES6?

ES6 destructuring assignment with more than one variable type

Destructuring assignment vs object property access in ES6

ES6 Destructuring assignment with `this`

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

Use of Colon in object assignment destructuring Javascript

Is it possible to assign an object while destructuring another (WHILE using defaults in destructuring)

What's the difference between object destructuring and normal object assignment in Javascript ES6?

Object destructuring: Assign null values in one line

ES6 destructuring, dynamic assignment

How to use deep destructuring on imports in ES6 syntax?

ES6 destructuring object, default value on assignment when not used as a function parameter?

How to assign one lambda expression object to another?

How to assign values from one object to another

Object property assignment with destructuring?

Destructuring assignment in object creation

Multilevel object Destructuring in ES6

Ternary condition in ES6 destructuring object

ES6 deep nested object destructuring

ES6 destructuring/cloning a subset of an object

ES6 Object Destructuring Default Parameters

ES6: destructuring an object with symbols as keys

es6 object destructuring not working

Destructuring nested object es6

How to inject one class into another for use as a constructor in es6 classes

How to namespace Destructuring assignment?