Object Combining

Javascript

Punchline

Combine arrays

let array1 = [1,2,3]; let array2 = [3,4,5]; let combined1 = [...array1, ...array2]; let combined2 = array1.concat(array2); // Both give: [1, 2, 3, 3, 4, 5]

Combine objects

let object1 = {a:1, b:2, c:3}; let object2 = {c:4, d:5, e:6}; let combined1 = { ...object1, ...object2 }; let combined2 = Object.assign({}, object1, object2); // Both give: {a: 1, b: 2, c: 4, d: 5, e: 6} Last one overrides the first!

Combine objects

Keep in mind, these are shallow copies, so modifying combined arrays/objects will affect their original versions. See Object Cloning for details.