Home > Enterprise >  remove key and values from array of objects
remove key and values from array of objects

Time:04-23

I want to remove or delete all object where inspectionScheduleQuestionId is null , how do we address this in JS ?

Thanks.

#data

const data = [
    {
        "id": 0,
        "inspectionScheduleQuestionId": 1,
        "inspectionScheduleConfirmationId": 0,
        "description": "65"
    },
    {
        "id": 0,
        "inspectionScheduleQuestionId": 10,
        "inspectionScheduleConfirmationId": 0,
        "description": "656"
    },
    {
        "id": 0,
        "inspectionScheduleQuestionId": null,
        "inspectionScheduleConfirmationId": 0,
        "description": 6
    },
    {
        "id": 0,
        "inspectionScheduleQuestionId": null,
        "inspectionScheduleConfirmationId": 0,
        "description": 21
    },

]

CodePudding user response:

You can use filter for this:

const updatedData = data.filter(item => item.inspectionScheduleQuestionId !== null);

Here's some more information on Array.prototype.filter.

CodePudding user response:

new data = data.filter(d => d.inspectionScheduleQuestionId)
  • Related