Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript convert Object.keys to destructuring vars of other object

Is there a way to convert Object.keys to vars can be destructured for other object ?

So if I have

let ob1 = { key1: 'value', key2: 'value2'}
let dictionary = { key1: 'super value', key2: 'other value' }

can do this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let { Object.keys(ob1) } = dictionary

>Solution :

Use Object.assign and map()

let ob1 = { key1: 'value', key2: 'value2'}
let dictionary = { key1: 'super value', key2: 'other value' }

let res = Object.assign(...Object.keys(ob1).map(key => ({ [key]: dictionary[key] })));
console.log(res);
{
  "key1": "super value",
  "key2": "other value"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading