Copy object values by key

How to copy the values from one object to other by it key name

by 3gwebtrain

JavaScript

const source = {
  key1: 'a',
  city:"Cyberjaya",
  common1: 'value1',
  a: {
    b: {
      c: {
        common2: 'value2'
      }
    }
  }
};

const dest = {
  key2: 'b',
  common1: null,
  common2: null,
  village:"madukkarai"
};

function extend(dest, source) {
  Object.entries(source).forEach(([key, value]) => {
    if (typeof value === 'object') {
      extend(dest, value);
    } else if (dest.hasOwnProperty(key)) {
      dest[key] = value;
    }
  });
}

extend(dest, source);
console.log(dest);