tableData consists of an array containing PDO objects. Each PDO object may have zero or more advocacy (pdo_advocacies), and each advocacy can contain zero or more programs (pdo_programs). For example:
// Array of PDO object
[
{
id: 1,
pdo_currency: "PHP",
name: "Gemango",
abbreviation: "Gemango",
dt_founded: "May. 02, 2024",
dt_created_at: "Jan. 01, 1970",
// Array of PDO advocacies
pdo_advocacies: [
{
id: 1,
advocacy: "PDO Operations",
pdo_id: 1,
// Array of PDO programs
pdo_programs: [
{
id: 6,
program: "PDO Budget",
pdo_advocacy_id: 1,
pdo_id: 1,
admin_beneficiary_category_id: 2,
beneficiary_category: {
id: 2,
name: "elders",
},
program_priority: {
id: 295,
name: "High",
},
program_category: null,
proposed_date: null,
funding_needs: "20000.00",
admin_beneficiary_category: {
id: 2,
category: "Children",
},
pdo_program_locations: [
{
id: 6,
pdo_program_id: 6,
location: {
id: 3,
name: "CEBU CITY, CEBU, PHILIPPINES",
},
funding_needs_location: "0.00",
unit_of_item: null,
num_of_items: 0,
num_of_beneficiaries: 0,
},
],
},
],
},
{
id: 2,
advocacy: "Unrestricted",
pdo_id: 1,
pdo_programs: [
{
id: 1,
program: "Unrestricted Funds",
pdo_advocacy_id: 2,
pdo_id: 1,
admin_beneficiary_category_id: null,
beneficiary_category: null,
program_priority: null,
program_category: null,
proposed_date: null,
funding_needs: "0.00",
admin_beneficiary_category: null,
pdo_program_locations: [
{
id: 1,
pdo_program_id: 1,
location: {
id: 1,
name: "Main",
},
funding_needs_location: "0.00",
unit_of_item: null,
num_of_items: 0,
num_of_beneficiaries: 0,
},
],
},
],
},
],
},
];
selectedOptions
[
{
"id": 2,
"name": "Children"
}
]
This code is designed to filter and return only the PDO, advocacy, and programs that have an admin_beneficiary_category.category value of "Children".
Filter Implementation
this.filteredTableData = this.tableData.filter((pdo) => {
return pdo.pdo_advocacies.some((advocacy) => {
return advocacy.pdo_programs.some((program) => {
return selectedOptions.some((option) => {
// Check if admin_beneficiary_category is null or undefined
if (program.admin_beneficiary_category === null || program.admin_beneficiary_category === undefined) {
// Check if the option name is 'null'
return option.name === 'null';
}
// Compare the program category name with the selected option name
return option.name === program.admin_beneficiary_category.category;
});
});
});
});
Actual Output
[...]
Expected Output:
[...]
Complete Code: https://stackblitz.com/edit/stackblitz-starters-azu5d2?file=src%2Fmain.ts