Q:

Delete an object based on two parameters from an array list

I want to delete this object
    {
        "taskNo": 1,
        "milestoneNo" : 1,
    }

From this whole array,
  "milestoneList" : [
{
    "milestoneTaskName" : {   
        "milestoneNo" : 1,
        "taskName": "test",  
    },
    "milestoneTask" : [
    {
        "taskNo": 1,
        "milestoneNo" : 1,
    },{
        "taskNo": 2,
        "milestoneNo" : 1,
    }
    ]
},
{
    "milestoneTaskName" : {
        "milestoneNo" : 2,
        "taskName": "test",
               },
    "milestoneTask" : [
    {  
        "taskNo": 1,
       "milestoneNo" : 2,
    },{
        "taskNo": 2,
        "milestoneNo" : 2,
    }
    ]
}
]

I have tried to do like this but it is not working. I have applied two condition in return but it does not return the actual output.
Here id is 1 and mileNo is 1
        milestoneList.map((obj)=>{
     if(typeof obj.milestoneTask !== 'undefined' && obj.milestoneTask.length > 0){
       obj.milestoneTask = obj.milestoneTask.filter(function(val) {
          return val["taskNo"] !== id && val["milestoneNo"] !== mileNo;
          });
         }
       })

A:

I think this is the compare statement you wanted
!(val["taskNo"] === id && val["milestoneNo"] === mileNo)

This will return false only if both the id and mileNo are matched. The statement you had returned false if either the id or mileNo were matched.

const milestoneList = [{
    "milestoneTaskName": {
      "milestoneNo": 1,
      "taskName": "test",
    },
    "milestoneTask": [{
      "taskNo": 1,
      "milestoneNo": 1,
    }, {
      "taskNo": 2,
      "milestoneNo": 1,
    }]
  },
  {
    "milestoneTaskName": {
      "milestoneNo": 2,
      "taskName": "test",
    },
    "milestoneTask": [{
      "taskNo": 1,
      "milestoneNo": 2,
    }, {
      "taskNo": 2,
      "milestoneNo": 2,
    }]
  }
]

const id = 1;
const mileNo = 1;

milestoneList.map((obj) => {
  if (typeof obj.milestoneTask !== 'undefined' && obj.milestoneTask.length > 0) {
    obj.milestoneTask = obj.milestoneTask.filter(function(val) {
      return !(val["taskNo"] === id && val["milestoneNo"] === mileNo);
    });
  }
})

console.log(milestoneList)

