Merge two objects but only existing properties

Here's a more functional approach using Array.prototype.reduce()

const obj1 = {
  "name": "",
  "age": ""
};

const obj2 = {
  "name": "Leo",
  "age": "14",
  "company": "aero",
  "shift": "night"
};

const newObject = Object.keys(obj1)
  .reduce(function(accumulator, key) {
    accumulator[key] = obj2[key]
    return accumulator
  }, {});

console.log(newObject);

Or some fun with ES6

const newObject = Object.keys(obj1)
  .reduce((a, key) => ({ ...a, [key]: obj2[key]}), {});

Update 2020

const data = {a:1,b:2,b:3}
const form = {a:'',b:''}

for (const key in data) {
  if (key in form) {
    form[key] = data[key];
  }
}
console.log(form) // Output: {a:1,b:2}

Assuming you only want enumerable properties, this is easily done with Object.keys and in (or hasOwnProperty):

Object.keys(obj2).forEach(function(key) {
    if (key in obj1) { // or obj1.hasOwnProperty(key)
        obj1[key] = obj2[key];
    }
});

Example:

var obj1 = {
  "name": "",
  "age": ""
};

var obj2 = {
  "name": "Leo",
  "age": "14",
  "company": "aero",
  "shift": "night"
};

Object.keys(obj2).forEach(function(key) {
  if (key in obj1) { // or obj1.hasOwnProperty(key)
    obj1[key] = obj2[key];
  }
});
console.log(obj1);

Or in ES2015 syntax (since you mentioned Object.assign):

for (const key of Object.keys(obj2)) {
    if (key in obj1) { // or obj1.hasOwnProperty(key)
        obj1[key] = obj2[key];
    }
}

Or a more fluent approach, but revisits the keys that are in obj1 (not that it's likely to matter:

Object.keys(obj2).filter(key => key in obj1).forEach(key => {
    obj1[key] = obj2[key];
});

Since forEach ignores the return value of its callback, we could even go further in the concise-land:

Object.keys(obj2).filter(key => key in obj1).forEach(key => obj1[key] = obj2[key]);

Tags:

Javascript