【问题标题】:Merging two objects using Nodejs使用 Nodejs 合并两个对象
【发布时间】:2022-01-08 13:37:26
【问题描述】:

我有两个 Json 对象。我想根据 Requirement_id 合并这些对象。第一个对象是需求的详细信息,另一个是匹配的详细信息。所以,我想将它与 Requirement_id 合并,如果有任何重复的类别值出现,我想将它合并到同一个 Requirement_id 下。 我正在使用 Nodejs14。

第一个对象

[
{
    "Requirement_id": 1,
    "Company_id": 884680,
    "Requirement_name": "Requirement 1",
    "Week_duration": 22,
    "Week_must_time": 22,
    "Hours_per_week": 22,
    "Hours_per_month": 2,
    "Hours_per_day": 2,
    "No_of_resources": 22,
    "Need_remote": "YES",
    "Documents": null,
    "Requirement_start": "2021-12-20",
    "Requirement_end": "2021-12-30",
    "Requirement_section": 1,
    "Project_id": 2,
    "Requirements_description": "sdsdsds",
    "User_id": 423101,
    "createdAt": "2021-12-10T15:00:48.000Z",
    "updatedAt": "2021-12-10T15:00:48.000Z"
},
{
    "Requirement_id": 2,
    "Company_id": 884680,
    "Requirement_name": "Application Designer",
    "Week_duration": 3,
    "Week_must_time": 3,
    "Hours_per_week": 3,
    "Hours_per_month": 3,
    "Hours_per_day": 3,
    "No_of_resources": 23,
    "Need_remote": "YES",
    "Documents": null,
    "Requirement_start": "2021-12-15",
    "Requirement_end": "2021-12-29",
    "Requirement_section": 2,
    "Project_id": 2,
    "Requirements_description": "sdsdsd",
    "User_id": 423101,
    "createdAt": "2021-12-10T15:18:25.000Z",
    "updatedAt": "2021-12-10T15:18:25.000Z"
},
{
    "Requirement_id": 6,
    "Company_id": 884680,
    "Requirement_name": "Sr Application Designer Head",
    "Week_duration": 3,
    "Week_must_time": 3,
    "Hours_per_week": 3,
    "Hours_per_month": 3,
    "Hours_per_day": 3,
    "No_of_resources": 4,
    "Need_remote": "YES",
    "Documents": null,
    "Requirement_start": "2021-12-20",
    "Requirement_end": "2022-01-31",
    "Requirement_section": 1,
    "Project_id": 1,
    "Requirements_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
    "User_id": 423121,
    "createdAt": "2021-12-11T13:22:10.000Z",
    "updatedAt": "2021-12-23T11:47:22.000Z"
},
 ]

第二个对象:

                [
            { Requirement_id: 1, Category: 'Technology', Matching: 2 },
            { Requirement_id: 2, Category: 'Technology', Matching: 1 },
            { Requirement_id: 9, Category: 'Technology', Matching: 1 },
            { Requirement_id: 6, Category: 'Technology', Matching: 3 },
            { Requirement_id: 13, Category: 'Domain', Matching: 1 },
            { Requirement_id: 8, Category: 'Roles', Matching: 1 },
            { Requirement_id: 9, Category: 'Roles', Matching: 1 },
            { Requirement_id: 6, Category: 'Roles', Matching: 1 },
            { Requirement_id: 10, Category: 'Roles', Matching: 2 },
            { Requirement_id: 13, Category: 'Qualifications', Matching: 1 }
            ]

预期输出:

               [{
                "Category":"Technology",
                "Matching":3,
                "Requirement_id": 1,
                "Company_id": 884680,
                "Requirement_name": "Requirement 1",
                "Week_duration": 22,
                "Week_must_time": 22,
                "Hours_per_week": 22,
                "Hours_per_month": 2,
                "Hours_per_day": 2,
                "No_of_resources": 22,
                "Need_remote": "YES",
                "Documents": null,
                "Requirement_start": "2021-12-20",
                "Requirement_end": "2021-12-30",
                "Requirement_section": 1,
                "Project_id": 2,
                "Requirements_description": "sdsdsds",
                "User_id": 423101,
                "createdAt": "2021-12-10T15:00:48.000Z",
                "updatedAt": "2021-12-10T15:00:48.000Z"
            },{
                "Category":"Technology",
                "Matching":1,
                "Requirement_id": 2,
                "Company_id": 884680,
                "Requirement_name": "Requirement 1",
                "Week_duration": 22,
                "Week_must_time": 22,
                "Hours_per_week": 22,
                "Hours_per_month": 2,
                "Hours_per_day": 2,
                "No_of_resources": 22,
                "Need_remote": "YES",
                "Documents": null,
                "Requirement_start": "2021-12-20",
                "Requirement_end": "2021-12-30",
                "Requirement_section": 1,
                "Project_id": 2,
                "Requirements_description": "sdsdsds",
                "User_id": 423101,
                "createdAt": "2021-12-10T15:00:48.000Z",
                "updatedAt": "2021-12-10T15:00:48.000Z"
            }]

【问题讨论】:

  • 我不明白你想如何合并它们。如果Requirement_id 是 9 怎么办?我们如何在TechnologiesRoles之间进行选择?

标签: node.js json


【解决方案1】:

如果我正确理解了您的问题,这应该会有所帮助

const firstArray = [
{
    "Requirement_id": 1,
    "Company_id": 884680,
    "Requirement_name": "Requirement 1",
    "Week_duration": 22,
    "Week_must_time": 22,
    "Hours_per_week": 22,
    "Hours_per_month": 2,
    "Hours_per_day": 2,
    "No_of_resources": 22,
    "Need_remote": "YES",
    "Documents": null,
    "Requirement_start": "2021-12-20",
    "Requirement_end": "2021-12-30",
    "Requirement_section": 1,
    "Project_id": 2,
    "Requirements_description": "sdsdsds",
    "User_id": 423101,
    "createdAt": "2021-12-10T15:00:48.000Z",
    "updatedAt": "2021-12-10T15:00:48.000Z"
},
{
    "Requirement_id": 2,
    "Company_id": 884680,
    "Requirement_name": "Application Designer",
    "Week_duration": 3,
    "Week_must_time": 3,
    "Hours_per_week": 3,
    "Hours_per_month": 3,
    "Hours_per_day": 3,
    "No_of_resources": 23,
    "Need_remote": "YES",
    "Documents": null,
    "Requirement_start": "2021-12-15",
    "Requirement_end": "2021-12-29",
    "Requirement_section": 2,
    "Project_id": 2,
    "Requirements_description": "sdsdsd",
    "User_id": 423101,
    "createdAt": "2021-12-10T15:18:25.000Z",
    "updatedAt": "2021-12-10T15:18:25.000Z"
},
{
    "Requirement_id": 6,
    "Company_id": 884680,
    "Requirement_name": "Sr Application Designer Head",
    "Week_duration": 3,
    "Week_must_time": 3,
    "Hours_per_week": 3,
    "Hours_per_month": 3,
    "Hours_per_day": 3,
    "No_of_resources": 4,
    "Need_remote": "YES",
    "Documents": null,
    "Requirement_start": "2021-12-20",
    "Requirement_end": "2022-01-31",
    "Requirement_section": 1,
    "Project_id": 1,
    "Requirements_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
    "User_id": 423121,
    "createdAt": "2021-12-11T13:22:10.000Z",
    "updatedAt": "2021-12-23T11:47:22.000Z"
},
 ]
 const secondArray =  [
            { Requirement_id: 1, Category: 'Technology', Matching: 2 },
            { Requirement_id: 2, Category: 'Technology', Matching: 1 },
            { Requirement_id: 9, Category: 'Technology', Matching: 1 },
            { Requirement_id: 6, Category: 'Technology', Matching: 3 },
            { Requirement_id: 13, Category: 'Domain', Matching: 1 },
            { Requirement_id: 8, Category: 'Roles', Matching: 1 },
            { Requirement_id: 9, Category: 'Roles', Matching: 1 },
            { Requirement_id: 6, Category: 'Roles', Matching: 1 },
            { Requirement_id: 10, Category: 'Roles', Matching: 2 },
            { Requirement_id: 13, Category: 'Qualifications', Matching: 1 }
            ]

const result = firstArray.map((fel)=>{
    let e = secondArray.find((sel)=>sel.Requirement_id===fel.Requirement_id)
    return e ? {...fel , ...e} : fel
})            
console.log(result)

【讨论】:

    【解决方案2】:

    由于您在secondArray中重复了类别,例如

    • Requirement_id:6、9 和 13

    我认为您需要在Categories 属性(或任何其他属性名称)中存储一组类别

    代码:

    const requirements = [
      { "Requirement_id": 1, "Company_id": 884680, "Requirement_name": "Requirement 1", "Week_duration": 22, "Week_must_time": 22, "Hours_per_week": 22, "Hours_per_month": 2, "Hours_per_day": 2, "No_of_resources": 22, "Need_remote": "YES", "Documents": null, "Requirement_start": "2021-12-20", "Requirement_end": "2021-12-30", "Requirement_section": 1, "Project_id": 2, "Requirements_description": "sdsdsds", "User_id": 423101, "createdAt": "2021-12-10T15:00:48.000Z", "updatedAt": "2021-12-10T15:00:48.000Z" },
      { "Requirement_id": 2, "Company_id": 884680, "Requirement_name": "Application Designer", "Week_duration": 3, "Week_must_time": 3, "Hours_per_week": 3, "Hours_per_month": 3, "Hours_per_day": 3, "No_of_resources": 23, "Need_remote": "YES", "Documents": null, "Requirement_start": "2021-12-15", "Requirement_end": "2021-12-29", "Requirement_section": 2, "Project_id": 2, "Requirements_description": "sdsdsd", "User_id": 423101, "createdAt": "2021-12-10T15:18:25.000Z", "updatedAt": "2021-12-10T15:18:25.000Z" },
      { "Requirement_id": 6, "Company_id": 884680, "Requirement_name": "Sr Application Designer Head", "Week_duration": 3, "Week_must_time": 3, "Hours_per_week": 3, "Hours_per_month": 3, "Hours_per_day": 3, "No_of_resources": 4, "Need_remote": "YES", "Documents": null, "Requirement_start": "2021-12-20", "Requirement_end": "2022-01-31", "Requirement_section": 1, "Project_id": 1, "Requirements_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "User_id": 423121, "createdAt": "2021-12-11T13:22:10.000Z", "updatedAt": "2021-12-23T11:47:22.000Z" }
    ]
    
    const categories = [
      { Requirement_id: 1, Category: 'Technology', Matching: 2 },
      { Requirement_id: 2, Category: 'Technology', Matching: 1 },
      { Requirement_id: 9, Category: 'Technology', Matching: 1 },
      { Requirement_id: 6, Category: 'Technology', Matching: 3 },
      { Requirement_id: 13, Category: 'Domain', Matching: 1 },
      { Requirement_id: 8, Category: 'Roles', Matching: 1 },
      { Requirement_id: 9, Category: 'Roles', Matching: 1 },
      { Requirement_id: 6, Category: 'Roles', Matching: 1 },
      { Requirement_id: 10, Category: 'Roles', Matching: 2 },
      { Requirement_id: 13, Category: 'Qualifications', Matching: 1 }
    ]
    
    const categoriesHash = categories.reduce((a, { Requirement_id: id, Category, Matching }) => {
      a[id] = a[id] || []
      a[id].push({ Category, Matching })
      return a
    }, {})
    
    const result = requirements.map(r => ({
      ...r,
      Categories: categoriesHash[r.Requirement_id]
    }))
    
    console.log(result)

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 2017-03-30
      相关资源
      最近更新 更多