#

Sunny Kushwaha

Admin

Angular Merge Objects Hack

Here's an example of how to merge objects: The JavaScript object spread operator is allows you to create a new object by copying the properties and values of an existing object. The syntax is three dots (...) followed by the object name

merge-object.component.ts
                                    
                                        // merge two object and create new object
                                        objectOne: Object = { name: 'sunny', age: 24};
                                        objectTwo: Object = { address: 'Pune', city: 'Pune'};
                                        objectthree: Object = { ...objectOne, ...objectTwo };
                                        console.log(objectthree, "Merged Object");

                                        // merge one object into other object
                                        obj1: Object = { a: 1, b: 2, c: 3 };
                                        obj2: Object = { ...obj1, d: 4 };
                                        console.log(obj2); // { a: 1, b: 2, c: 3, d: 4 }

                                    
                                

In this example, the object spread operator is used to create a new object obj2 by copying the properties and values of obj1 and adding a new property d with a value of 4.

That's all you need to merge objects my friend!