Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created March 7, 2013 11:05
Show Gist options
  • Save nfreear/5107307 to your computer and use it in GitHub Desktop.
Save nfreear/5107307 to your computer and use it in GitHub Desktop.
Concatenate multiple Javascript objects.
//http://stackoverflow.com/questions/2454295/javascript-concatenate-properties-from-multiple-objects-associative-array
function Collect(ob1, ob1) {
var ret = {},
len = arguments.length,
arg,
i = 0,
p;
for (i = 0; i < len; i++) {
arg = arguments[i];
if (typeof arg !== "object") {
continue;
}
for (p in arg) {
if (arg.hasOwnProperty(p)) {
ret[p] = arg[p];
}
}
}
return ret;
}
var out = Collect({ a:3 }, { b:"C" }, { b:10 });
console.log(out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment