merging objects

PHOTO EMBED

Mon Mar 01 2021 03:26:59 GMT+0000 (Coordinated Universal Time)

Saved by @MattMoniz #javascript #objects

function merge(...objects) {
  let masterObj = {}

  // iterate over `objects` merging each
  // into `masterObj` to generate flattened
  // object
  for (let i = 0; i < objects.length; i++) {
    let obj = objects[i]
    for (let key in obj) masterObj[key] = obj[key]
  }

  return masterObj
}

let merged = merge(...objectsList)

// output:
// {count:5, delay:2000, early:false, message:'Hello'}
console.log(merged)
content_copyCOPY

https://www.benmvp.com/blog/learning-es6-parameter-handling/#spread-operator