JS

HOW TO MERGE OBJECTS IN JAVASCRIPT

var objA = {id: 999, name: "Jon Doe"}; var objB = {email: "jon@doe.com"}; var combined = {...objA, ...objB};

SPREAD OPERATOR

01

var objA = {id: 999, name: "Jon Doe"}; var objB = {email: "jon@doe.com"}; var combined = Object.assign(objA, objB);

OBJECT ASSIGN

02

var objA = {id: 999, name: "Jon Doe"}; var objB = {email: "jon@doe.com"};   var combined = {}; for (var key in objA) { combined[key] = objA[key]; } for (var key in objB) { combined[key] = objB[key]; }

MANUAL COMBINE

03