1

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 :

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
1
  • Besides the "class_id": "COMPRO1", Delete all nulls from the data Commented Nov 17, 2021 at 7:32

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

TypeError: dd.filter is not a function
its the whole data which is the one that I am referring at the top
Ohh, you should just filter the duties_details,map won't change the array's length, just transform every element. so maybe drdata[0].duties_details = drdata[0].duties_details.filter(e => e.accmp != null) instead of drdata[0].duties_details.map((dd) => .... I'll edit my answere
life-saving!, thanks
0

I wrote a whole case

const deepClone = obj => {
  let clone = Object.assign({}, obj);
  Object.keys(clone).forEach(
    key => {
      if (obj[key] == null) {
        delete obj[key]
      } else if (typeof obj[key] === 'object') {
        deepClone(obj[key])
      } else {
        return obj[key]
      }
    }
  );
  if (Array.isArray(obj)) {
    clone.length = obj.length;
    return Array.from(clone);
  }
  return clone;
};

const hasArr = (arr = []) => {
  const clone=[]
  arr.forEach(val => {
    if (val.class_id != 'COMPRO1') {
      clone.push(deepClone(val))
    }
  })
  return clone
}
console.log(JSON.stringify(hasArr(arr), null, 4));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.