Home > Software engineering >  How to delete an empty property of an object?
How to delete an empty property of an object?

Time:01-20

I'm new in JS and I want to delete the property "" of an object like this:

{tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}

to

{tecnico2: 1.1, tecnico4: 5, tecnico1: 3, tecnico3: 3}

Thank you for your time!!

I search about this but I only saw examples about delete the values '', not the key like I need it.

CodePudding user response:

const data = {tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}

delete data['']

console.log(data)

or:

const data = {tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}

console.log(Object.fromEntries(Object.entries(data).filter(([k])=>k!=='')))

CodePudding user response:

const start = {tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}
        
const { ['']: empty, ...arrayWithoutKey } = start

console.log(arrayWithoutKey)
// { tecnico2: 1.1, tecnico4: 5, tecnico1: 3, tecnico3: 3 }
  • Related