Home > Mobile >  Merging two objects using Nodejs
Merging two objects using Nodejs

Time:01-08

I have two Json objects. I want to merge these objects according to the Requirement_id. The first Object is the details of requirement and other one is matching details.So, I want to merge it with respect to the Requirement_id and if any duplicate Category value coming, I want to merge it under the same Requirement_id. I am using Nodejs14.

First Object

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

Second object:

                [
            { 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 }
            ]

Exepcted output:

               [{
                "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"
            }]

CodePudding user response:

If I correctly understood your question this should help

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)

CodePudding user response:

Since you have repeated Categories in secondArray, for example

  • Requirement_id: 6, 9 and 13

I think what you need is to store an array of categories in Categories property (or any other property name)

Code:

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, c) => {
  a[c.Requirement_id] = a[c.Requirement_id] || []
  a[c.Requirement_id].push({
    Category: c.Category,
    Matching: c.Matching
  })
  return a
}, {})

const result = requirements.map(r => ({
  ...r,
  Categories: categoriesHash[r.Requirement_id]
}))

console.log(result)

  •  Tags:  
  • Related