Question:

Write a function called mergeObjects() that takes a variable number of objects as parameters and returns a single object with all the properties from the individual objects. mergeObjects({some: "thing"}, {cool: "jerk", phat: "cow"}, {holy: "moly"}) should return {some: "thing", cool: "jerk", phat: "cow", holy: "moly"}.

Answer:

function mergeObjects () {
  var result = arguments[0];
  for (var i = 1, l = arguments.length; i < l; i ++) {
    var arg = arguments[i];
    for (var key in arg) {
      result[key] = arg[key];
    }
  }
  return result;
}

Keywords:

© 2017 QuizBucket.org