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

Combine Object/Array using Vanilla JS

How do you combine two objects like this?

const target = { year1 : {}, year2: {somevalue2...}};
const source = { year1 : {somevalue1...}, year2: {} };

expected Input:

{ year1 : {somevalue1}, year2 : {somevalue2} }

Thank you!

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

>Solution :

Use lodash _.merge

const target = { year1 : {}, year2: {s:1}}
const source = { year1 : {s:2}, year2: {} }
const result = _.merge(target, source);
console.log('result', result)
// => { year1 : {s:2}, year2: {s:1}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
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