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

Remove a null value from nested array of objects with conditions

I have a data like this:

[
    {
        "title": "Chemist I",
        "class_id": "CMT1",
        "deptcode": "001",
        "c_title_id": 978,
        "duties_details": [
            {
                "accmp": [
                    {
                        "d_c": "2021-11-17T14:43:22",
                        "date": "Nov 17, 2021",
                        "empid": 67,
                        "posdrid": 48,
                        "remarks": "Net",
                        "accomp_id": 46,
                        "month_year": "NOVEMBER , 2021",
                        "approved_by": null,
                        "date_created": "2021-11-14T14:43:00",
                        "pc_review_item_id": 2
                    }
                ],
                "prcnt": 15,
                "dscrptn": "Troubleshoot Computer",
                "pos_drid": 48
            },
            {
                "accmp": null,
                "prcnt": 10,
                "dscrptn": "Test CMT Data",
                "pos_drid": 49
            }
        ]
    }
],
[
    {
        "title": "Computer Programmer I",
        "class_id": "COMPRO1",
        "deptcode": "001",
        "c_title_id": 177,
        "duties_details": [
            {
                "accmp": [
                    {
                        "d_c": "2021-11-17T14:43:08",
                        "date": "Nov 17, 2021",
                        "empid": 67,
                        "posdrid": 40,
                        "remarks": "test",
                        "accomp_id": 45,
                        "month_year": "NOVEMBER , 2021",
                        "approved_by": null,
                        "date_created": "2021-11-15T14:43:00",
                        "pc_review_item_id": 1
                    }
                ],
                "prcnt": 30,
                "dscrptn": "Develop system procedures/methods/functions based on the system design and report to the immediate head once completed",
                "pos_drid": 40
            },
            {
                "accmp": null,
                "prcnt": 15,
                "dscrptn": "Write code for software patches and bug fixes",
                "pos_drid": 42
            },
            {
                "accmp": null,
                "prcnt": 15,
                "dscrptn": "Run series/level of software tests to spot and resolve logical(bug) & syntactical errors",
                "pos_drid": 41
            },
            {
                "accmp": null,
                "prcnt": 10,
                "dscrptn": "Write encountered/reported/spotted system issues/errors/bugs into the issue features of version control system",
                "pos_drid": 45
            },
            {
                "accmp": null,
                "prcnt": 10,
                "dscrptn": "Write documentations to the created system procedures/methods/functions",
                "pos_drid": 44
            },
            {
                "accmp": null,
                "prcnt": 10,
                "dscrptn": "Review created system procedures/methods/functions for system efficiency",
                "pos_drid": 43
            },
            {
                "accmp": null,
                "prcnt": 10,
                "dscrptn": "Commit and push every complete & working changes made to the source code into the version control system",
                "pos_drid": 46
            }
        ]
    }
]    

now based on the data, I want to remove the duties_details with a value of null from other positions but not in "class_id": "COMPRO1".

{
    "accmp": null,
    "prcnt": 10,
    "dscrptn": "Test CMT Data",
    "pos_drid": 49
}

I have tried like this but It returns undefined :

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

let filterDRData = DRData ? DRData.map((drdata) => {        
        if(drdata[0].class_id != "COMPRO1") {
            drdata[0].duties_details.map((dd) => {                
                return {
                    ...drdata,
                    duties_details:dd.filter(e => e.accmp == null)                      
                }
                    
            })
        }
    }):null

>Solution :

You outer lambda does not return anything.

this should work:

let filterDRData = DRData ? DRData.map((drdata) => {        
    if(drdata[0].class_id != "COMPRO1") {
        drdata[0].duties_details = drdata[0].duties_details.filter(e => e.accmp != null);
    }
    return drdata; // ADD THIS LINE
}):null
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