Q:

Concatenate JS objects

I have two object like below
var data = {
        site_name: "my_site",
        page_id: "page_name",
        city: "New York",
        region_code: "NY",
        country_code: "US"
     },
     user_data = {
        user: "user_name",
        conversion_rate: 1.25
     };

I want to concatenate these two object like below
user_data = {
     user: "user_name",
     conversion_rate: 1.25,
     site_name: "my_site",
     page_id: "page_name",
     city: "New York",
     region_code: "NY",
     country_code: "US"
 };

I did it by looping on second on by checking typeof user_data.key .... and it solved my problem but my curiosity is to do it without looping in an optimized way. Any of your suggestion will be appreciated.  

A:

You can use Object.assign of ES6:
var combined = Object.assign({}, data, user_data);

Or even:
Object.assign(user_data, data); // this will change `user_data`

