How make nested array of objects into the desired array of elements

Input:

const Input = {
    branchStudent: [
        { id: 1, branchId: 1, studentId: 1 },
        { id: 2, branchId: 2, studentId: 2 },
        { id: 3, branchId: 2, studentId: 1 },
        { id: 4, branchId: 1, studentId: 3 }
    ],
    branch: [
        { id: 1, name: "CSE" },
        { id: 2, name: "IT" }
    ],
    student: [
        { id: 1, name: "Jay" },
        { id: 2, name: "Sanjay" },
        { id: 3, name: "Rajesh" }
    ]
}

Output:

branchStudentMap: [
    { id: 1, branchId: 1, branchName: "CSE", studentId: 1, studentName: "Jay" },
    { id: 2, branchId: 2, branchName: "IT", studentId: 2, studentName: "Sanjay" },
    { id: 3, branchId: 2, branchName: "IT", studentId: 1, studentName: "Jay" },
    { id: 4, branchId: 1, branchName: "CSE", studentId: 3, studentName: "Rajesh" }
]

You loop over your branchStudents, get the IDs for the other objects and loop over them until you get a match whereas you cope the values into the new object.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.