0

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"
            }]
1
  • 1
    I didn't understand how you want to merge them. What if the Requirement_id is 9? How do we chose between Technologiesand Roles? Commented Jan 7, 2022 at 16:42

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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, { 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.