How to delete property from spread operator?

You could use Rest syntax in Object Destructuring to get all the properties except drugName to a rest variable like this:

const transformedResponse = [{
    drugName: 'HYDROCODONE-HOMATROPINE MBR',
    drugStrength: '5MG-1.5MG',
    drugForm: 'TABLET',
    brand: false
},
{
    drugName: 'HYDROCODONE ABC',
    drugStrength: '10MG',
    drugForm: 'SYRUP',
    brand: true
}]

const output = transformedResponse.map(({ drugName, ...rest }) => rest)

console.log(output)

Also, when you spread an array inside {}, you get an object with indices of the array as key and the values of array as value. This is why you get an object with 0 as key in loggerResponse:

const array = [{ id: 1 }, { id: 2 }]
console.log({ ...array })

Leave a Comment