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!

>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>

Leave a Reply