Sample JSON Data:
const data = [{
"_id": "5cc5df25a193c21608666b00",
"departmentName": "Software Dev",
"managerId": "5cc5df25a193c21608666b01",
"managerEmail": "[email protected]",
"teams": [{
"teamName": "SWD Team 1",
"teamLeadId": "5cc5df25a193c21608666b02",
"teamLeadEmail": "[email protected]",
"teamMembers": [{
"memberId": "5cc5df25a193c21608666b03",
"memberEmail": "[email protected]"
}]
},
{
"teamName": "SWD Team 2",
"teamLeadId": "5cc5df25a193c21608666b04",
"teamLeadEmail": "[email protected]",
"teamMembers": [{
"memberId": "5cc5df25a193c21608666b05",
"memberEmail": "[email protected]"
},
{
"memberId": "5cc5df25a193c21608666b06",
"memberEmail": "[email protected]"
}
]
}
]
},
{
"_id": "5cc5df25a193c21608666b07",
"departmentName": "Software QA",
"managerId": "5cc5df25a193c21608666b08",
"managerEmail": "[email protected]",
"teams": [{
"teamName": "QA Team 1",
"teamLeadId": "5cc5df25a193c21608666b09",
"teamLeadEmail": "[email protected]",
"teamMembers": [{
"memberId": "5cc5df25a193c21608666b10",
"memberEmail": "[email protected]"
}]
},
{
"teamName": "QA Team 2",
"teamLeadId": "5cc5df25a193c21608666b11",
"teamLeadEmail": "[email protected]",
"teamMembers": []
},
{
"teamName": "QA Team 3",
"teamLeadId": "5cc5df25a193c21608666b12",
"teamLeadEmail": "[email protected]",
"teamMembers": [{
"memberId": "5cc5df25a193c21608666b13",
"memberEmail": "[email protected]"
},
{
"memberId": "5cc5df25a193c21608666b14",
"memberEmail": "[email protected]"
},
{
"memberId": "5cc5df25a193c21608666b15",
"memberEmail": "[email protected]"
}
]
}
]
},
{
"_id": "5cc5df25a193c21608666b16",
"departmentName": "Software Creative",
"managerId": "5cc5df25a193c21608666b17",
"managerEmail": "[email protected]",
"teams": []
},
{
"_id": "5cc5df25a193c21608666b18",
"departmentName": "Software BA",
"managerId": "5cc5df25a193c21608666b19",
"managerEmail": "[email protected]",
"teams": [{
"teamName": "BA Team 1",
"teamLeadId": "5cc5df25a193c21608666b20",
"teamLeadEmail": "[email protected]",
"teamMembers": []
}]
}
];
Desc: There're three levels. Top = Department, Middle = Team, and Bottom = Team Members. One department can have 0 or more Teams. One team can have 0 or more Team Members.
Goal: I'm trying to ONLY return the data where teamMembers array has at least one value populated.
So what should be eliminated in the output is 1) Software QA/QA Team 2- just the QA Team 2 because the other QA Teams have teamMembers 2) Software Creative - the entire object because it doesn't even have a team, let alone a teamMember, and 3) Software BA - has one team, but no teamMembers. These three objects do NOT have a single value populated inside teamMembers array.
I feel like I need to use Array.prototype.filter() but not sure how I go about it with nested objects (#JSnewbie). Also, I'm learning JS right now so some suggestions with the most current ways to accomplish this would be awesome.
Desired Output:
[
{
"_id": "5cc5df25a193c21608666b00",
"departmentName": "Software Dev",
"managerId": "5cc5df25a193c21608666b01",
"managerEmail": "[email protected]",
"teams": [
{
"teamName": "SWD Team 1",
"teamLeadId": "5cc5df25a193c21608666b02",
"teamLeadEmail": "[email protected]",
"teamMembers": [
{
"memberId": "5cc5df25a193c21608666b03",
"memberEmail": "[email protected]"
}
]
},
{
"teamName": "SWD Team 2",
"teamLeadId": "5cc5df25a193c21608666b04",
"teamLeadEmail": "[email protected]",
"teamMembers": [
{
"memberId": "5cc5df25a193c21608666b05",
"memberEmail": "[email protected]"
},
{
"memberId": "5cc5df25a193c21608666b06",
"memberEmail": "[email protected]"
}
]
}
]
},
{
"_id": "5cc5df25a193c21608666b07",
"departmentName": "Software QA",
"managerId": "5cc5df25a193c21608666b08",
"managerEmail": "[email protected]",
"teams": [
{
"teamName": "QA Team 1",
"teamLeadId": "5cc5df25a193c21608666b09",
"teamLeadEmail": "[email protected]",
"teamMembers": [
{
"memberId": "5cc5df25a193c21608666b10",
"memberEmail": "[email protected]"
}
]
},
{
"teamName": "QA Team 3",
"teamLeadId": "5cc5df25a193c21608666b12",
"teamLeadEmail": "[email protected]",
"teamMembers": [
{
"memberId": "5cc5df25a193c21608666b13",
"memberEmail": "[email protected]"
},
{
"memberId": "5cc5df25a193c21608666b14",
"memberEmail": "[email protected]"
},
{
"memberId": "5cc5df25a193c21608666b15",
"memberEmail": "[email protected]"
}
]
}
]
}
]