简体   繁体   中英

Union more than one object into one object

I have two object returned from database, i want to union this object into one object, for example:

{
            "id": 4,
            "first_name": "s",
            "last_name": "a",
            "personal_no": "12",
            "email": "",
            "gender": "M",
            "address": "s",
            "birthdate": "a",
            "phone": "12"
        }

second one :

{
            "id": 684,
            "create_date": "2017-08-31T07:41:32.000Z",
            "update_date": null,
            "person_no": "",
            "status_id": 1,
            "payment_status_id": null,
            "payment_type": null,
            "comment": null,
            "res_type": null
        }

I want to get result like this

{
{
first object 
},
{
second object
}
}

you can use an array to do this, and then use Object.assign() to convert it into an object

 let a = { "id": 4, "first_name": "aa", "last_name": "a", "personal_no": "a", "email": "a", "gender": "MALE", "address": "a", "birthdate": "2017-09-14T20:00:00.000Z", "phone": "a" }; let b = { "id": 684, "create_date": "2017-08-31T07:41:32.000Z", "update_date": null, "person_no": "a", "status_id": 1, "payment_status_id": null, "payment_type": null, "comment": null, "res_type": null } let result = [a, b]; console.log(result); let result2 = Object.assign({}, result); console.log(result2);

I think your expectation is as follows

 var data={ "firstObject":{}, "secondObject":{} }; data.firstObject={ "id": 4, "first_name": "s", "last_name": "s", "personal_no": "1", "email": "a", "gender": "MALE", "address": "a", "birthdate": "a", "phone": "a" }; data.secondObject={ "id": 684, "create_date": "a", "update_date": null, "person_no": "a", "status_id": 1, "payment_status_id": null, "payment_type": null, "comment": null, "res_type": null }; console.log(data.firstObject); console.log(data.secondObject);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM