skip to Main Content

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.

2

Answers


  1. const data = {tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}
    
    delete data['']
    
    console.log(data)

    or, if you don’t want to modify the original data object:

    const data = {tecnico2: 1.1, "": 0, tecnico4: 5, tecnico1: 3, tecnico3: 3}
    
    console.log(Object.fromEntries(Object.entries(data).filter(([k])=>k!=='')))
    Login or Signup to reply.
  2. 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 }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search