Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript Filter 3rd level nested array of object

I have the json data and am trying to filter the nested array object which have in 3rd level.

Sample data:

   var data = {
    "cList": [
        {
            "Id": "111111",
            "Number": "176116",
            "Name": "Test",
            "gList": [
                {


                    "gNumber": "123456",
                    "productList": [
                        {
                            "spn": "2118593162",
                            "pno": "2118593163",
                            "effectiveDate": "12/12/2023",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2131493390",
                            "pno": "2131493390",
                            "effectiveDate": "01/01/2020",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2100171794",
                            "pno": "2100171794",
                            "benefitPlanType": "Wellness Programs",
                            "effectiveDate": "01/01/2023",
                            "partB": false,
                            "id": ""
                        }
                    ]
                },
                {
                    "gNumber": "10422798",
                    "productList": [
                        {
                            "spn": "2118593162",
                            "pno": "2118593163",
                            "effectiveDate": "01/01/2022",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2131493390",
                            "pno": "2131493390",
                            "effectiveDate": "01/01/2020",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2100171794",
                            "pno": "2100171794",
                            "benefitPlanType": "Wellness Programs",
                            "effectiveDate": "01/01/2023",
                            "partB": false,
                            "id": ""
                        }
                    ],
                },
                {
                    "gNumber": "10422700",
                    "productList": [
                        {
                            "spn": "2118593162",
                            "pno": "2118593163",
                            "effectiveDate": "01/01/2023",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2131493390",
                            "pno": "2131493390",
                            "effectiveDate": "01/01/2020",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2100171794",
                            "pno": "2100171794",
                            "benefitPlanType": "Wellness Programs",
                            "effectiveDate": "01/01/2023"
                        },
                    ],
                }
            ]
        },
        {
            "Id": "222",
            "Number": "176116",
            "Name": "Test",
            "gList": [
                {
                    "gNumber": "1234567",
                    "productList": [
                        {
                            "spn": "2118593162",
                            "pno": "2118593163",
                            "effectiveDate": "12/12/2010",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2131493390",
                            "pno": "2131493390",
                            "effectiveDate": "01/01/2020",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2100171794",
                            "pno": "2100171794",
                            "benefitPlanType": "Wellness Programs",
                            "effectiveDate": "01/01/2023",
                            "partB": false,
                            "id": ""
                        }
                    ]
                },
                {
                    "gNumber": "10422798",
                    "productList": [
                        {
                            "spn": "2118593162",
                            "pno": "2118593163",
                            "effectiveDate": "01/01/2022",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2131493390",
                            "pno": "2131493390",
                            "effectiveDate": "01/01/2020",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2100171794",
                            "pno": "2100171794",
                            "benefitPlanType": "Wellness Programs",
                            "effectiveDate": "01/01/2023",
                            "partB": false,
                            "id": ""
                        }
                    ],
                },
                {
                    "gNumber": "10422795",
                    "productList": [
                        {
                            "spn": "2118593162",
                            "pno": "2118593163",
                            "effectiveDate": "12/12/2023",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2131493390",
                            "pno": "2131493390",
                            "effectiveDate": "01/01/2020",
                            "partB": false,
                            "id": ""
                        },
                        {
                            "spn": "2100171794",
                            "pno": "2100171794",
                            "benefitPlanType": "Wellness Programs",
                            "effectiveDate": "01/01/2023"
                        },
                    ],
                }
            ]
        }
    ],
};

I am trying to get the gNumber’s only which have in the productList array effectiveDate = "12/12/2023";

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

or

Get the first matched result also fine.

const filteredClientList = data.cList.filter(
    (cListElement) =>
        cListElement.gList.find(
            (gListElement) =>
                gListElement.productList.filter((productListElement) => productListElement.effectiveDate === "12/12/2023"),
        ),
);
console.log("filteredClientList", JSON.stringify(filteredClientList));

Expected output:

[
  {
    "gNumber": "123456",
    "productList": [
      {
        "spn": "2118593162",
        "pno": "2118593163",
        "effectiveDate": "12/12/2023",
        "partB": false,
        "id": ""
      },
      {
        "spn": "2131493390",
        "pno": "2131493390",
        "effectiveDate": "01/01/2020",
        "partB": false,
        "id": ""
      },
      {
        "spn": "2100171794",
        "pno": "2100171794",
        "benefitPlanType": "Wellness Programs",
        "effectiveDate": "01/01/2023",
        "partB": false,
        "id": ""
      }
    ]
  },
  {
    "gNumber": "10422795",
    "productList": [
      {
        "spn": "2118593162",
        "pno": "2118593163",
        "effectiveDate": "12/12/2023",
        "partB": false,
        "id": ""
      },
      {
        "spn": "2131493390",
        "pno": "2131493390",
        "effectiveDate": "01/01/2020",
        "partB": false,
        "id": ""
      },
      {
        "spn": "2100171794",
        "pno": "2100171794",
        "benefitPlanType": "Wellness Programs",
        "effectiveDate": "01/01/2023"
      }
    ]
  }
]

>Solution :

flatMap followed by a filter with a some inside.
flatMap is equivalent to doing a map followed by a flat().
flatMap gets you the all data in a[{gNumber,productList},...]. Now to filter it the condition must be at least one of productList should have the given effectiveDate. For that some is used

var data = { "cList": [ { "Id": "111111", "Number": "176116", "Name": "Test", "gList": [ {                "gNumber": "123456",                "productList": [                    {                        "spn": "2118593162",                        "pno": "2118593163",                        "effectiveDate": "12/12/2023",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2131493390",                        "pno": "2131493390",                        "effectiveDate": "01/01/2020",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2100171794",                        "pno": "2100171794",                        "benefitPlanType": "Wellness Programs",                       "effectiveDate": "01/01/2023",                        "partB": false,                        "id": ""                    }                ]            },            {                "gNumber": "10422798",                "productList": [                    {                        "spn": "2118593162",                        "pno": "2118593163",                        "effectiveDate": "01/01/2022",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2131493390",                        "pno": "2131493390",                        "effectiveDate": "01/01/2020",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2100171794",                        "pno": "2100171794",                        "benefitPlanType": "Wellness Programs",                        "effectiveDate": "01/01/2023",                        "partB": false,                        "id": ""                    }                ],            },            {                "gNumber": "10422700","productList": [                    {                        "spn": "2118593162",                        "pno": "2118593163",                        "effectiveDate": "01/01/2023",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2131493390",                        "pno": "2131493390",                        "effectiveDate": "01/01/2020",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2100171794",                        "pno": "2100171794",                        "benefitPlanType": "Wellness Programs",                        "effectiveDate": "01/01/2023"                    },                ],            }        ]    },    {        "Id": "222",        "Number": "176116",        "Name": "Test",        "gList": [            {                "gNumber": "1234567",                "productList": [                    {                        "spn": "2118593162",                        "pno": "2118593163",                        "effectiveDate": "12/12/2010",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2131493390",                        "pno": "2131493390",                        "effectiveDate": "01/01/2020",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2100171794",                        "pno": "2100171794",                        "benefitPlanType": "Wellness Programs",                        "effectiveDate": "01/01/2023",                        "partB": false,                        "id": ""                    }                ]            },            {                "gNumber": "10422798",                "productList": [                    {                        "spn": "2118593162",                        "pno": "2118593163",                        "effectiveDate": "01/01/2022",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2131493390",                        "pno": "2131493390",                        "effectiveDate": "01/01/2020",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2100171794",                        "pno": "2100171794",                        "benefitPlanType": "Wellness Programs",                        "effectiveDate": "01/01/2023",                        "partB": false,                        "id": ""                    }                ],            },            {                "gNumber": "10422795",                "productList": [                    {                        "spn": "2118593162",                        "pno": "2118593163",                        "effectiveDate": "12/12/2023",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2131493390",                        "pno": "2131493390",                        "effectiveDate": "01/01/2020",                        "partB": false,                        "id": ""                    },                    {                        "spn": "2100171794",                        "pno": "2100171794",                        "benefitPlanType": "Wellness Programs",                        "effectiveDate": "01/01/2023"                    },                ],            }        ]    }],}

const filtered = data.cList.flatMap(x => x.gList).filter(y => y.productList.some(z => z.effectiveDate === '12/12/2023') )

console.log(filtered)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading