嵌套多个Json对象时合并它们

kxkpmulp  于 2023-02-01  发布在  其他
关注(0)|答案(2)|浏览(194)

我目前正在开发一个端点,我想从4个不同的表中返回一个对象。我从sequelize中获得每个表的JSON格式记录。我有一个位置、服务、员工和租户。我如何将它们合并为一个嵌套的对象。例如:我从sequelize工作人员那里得到的数据是:

{
    "id": 2,
    "profession": "Dentist",
    "note": "Good Man",
    "holidays": "Saturday",
    "specialDays": null,
    "createdAt": "2023-01-27T14:23:52.000Z",
    "updatedAt": "2023-01-27T14:23:52.000Z",
}

所有其他数据都是类似的格式,但我想合并它们,返回类似于以下内容的结果:

{  staff:{
        "id": 2,
        "profession": "Dentist",
        "note": "Good Man",
        "holidays": "Saturday",
        "specialDays": null,
        "createdAt": "2023-01-27T14:23:52.000Z",
        "updatedAt": "2023-01-27T14:23:52.000Z",},
    location:{
        "id": 1,
        "name": "Branch 1",
        "address": "37 Automatic Handling",
        "description": "This is our main branch",
        "latitude": "564233",
        "longtitude": "4441256",
        "visible": true,
        "createdAt": "2023-01-27T14:05:37.000Z",
        "updatedAt": "2023-01-27T14:05:37.000Z",
    }
           
       

     }
4uqofj5v

4uqofj5v1#

你自己做一个东西

// (inside of an async function)

const location = await // sequelize code to get this info
const service = await // sequelize code to get this info
const staff = await // sequelize code to get this info
const tenant = await // sequelize code to get this info

return res.json({
  location,
  service,
  staff,
  Tenant: tenant, // remove "Tenant: " if the capital was a typo or you don't care
});
ltskdhd1

ltskdhd12#

这个应该可以

const staff = {
    "id": 2,
    "profession": "Dentist",
    "note": "Good Man",
    "holidays": "Saturday",
    "specialDays": null,
    "createdAt": "2023-01-27T14:23:52.000Z",
    "updatedAt": "2023-01-27T14:23:52.000Z",
};

const location1 = {
    "id": 1,
    "name": "Branch 1",
    "address": "37 Automatic Handling",
    "description": "This is our main branch",
    "latitude": "564233",
    "longtitude": "4441256",
    "visible": true,
    "createdAt": "2023-01-27T14:05:37.000Z",
    "updatedAt": "2023-01-27T14:05:37.000Z",
};

const mergedObject = { staff, location1 };
console.log(mergedObject);

相关问题