Home > Net >  Merge Multiple Objects (es5)
Merge Multiple Objects (es5)

Time:09-02

I have a requirement to merge two (or Multiple) Objects as a single object. While keeping each Object name as the parent key.

var event = 
  { id   : '45243'
  , name : 'Cardiff locè'
  , loc  : 'Cardiff'
  }

var alert = 
  { node     : 'sdwan edge'
  , severity : 'critical'
  }

The output should be like this:

var mergedObject = 
 { event :
    { id   : '45243'
    , name : 'Cardiff loc'
    , loc  : 'Cardiff'
    }
 , alert:
    { node     : 'sdwan edge'
    , severity : 'critical'
    }
 }

CodePudding user response:

mergeObject.event = event;
mergeObject.alert = alert;

CodePudding user response:

That's not really merging. The existing objects are unchanged. You are just creating a new object.

var mergedObject = {
    "event": event,
    "alert": alert
};

CodePudding user response:

var event = {
  "id": "45243",
  "name": "Cardiff loc",
  "loc": "Cardiff"
}
var alert = {
  "node": "sdwan edge",
  "severity": "critical"
}


var mergedObject = {
  event,
  alert
};

console.log(mergedObject)

  • Related