【问题标题】:Merge multiple Json objects while nesting them嵌套时合并多个 Json 对象
【发布时间】:2023-01-29 16:39:51
【问题描述】:

我目前正在开发一个端点,我想从 4 个不同的表中返回一个对象。我从 sequelize 中获取 JSON 格式的每个表的记录。我有位置、服务、员工和租户。如何将它们合并为一个对象但嵌套。 例如: 我从 sequelize for 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",
}

所有其他数据都采用类似的格式。但我想合并它们以返回类似的内容:

 {  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",
    }
           
       

     }

【问题讨论】:

    标签: javascript json express sequelize.js


    【解决方案1】:

    自己做一个对象

    // (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
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 2021-04-13
      • 2018-10-25
      相关资源
      最近更新 更多