DEV Community

bomoniyi
bomoniyi

Posted on

How to merge object

const user = {
  name: "",
  username: "",
  phoneNumber: "",
  email: "",
  password: ""  
};

const newUser = {
  username: "ReedBarger",
  email: "reed@gmail.com",
  password: "mypassword"  
};

console.log(Object.assign({}, user, newUser));

Enter fullscreen mode Exit fullscreen mode

(1) Always start with an empty block {} so you don't overwrite 'user'.
(2) Don't forget that objects are passed as references and not values.

Top comments (0)