-2

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.

Dany S
  • 5
  • 1
  • also [Remove value from object without mutation](https://stackoverflow.com/questions/33053310/remove-value-from-object-without-mutation) and a 14 year old duplicate [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – pilchard Jan 19 '23 at 20:40

2 Answers2

0

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!=='')))
Andrew Parks
  • 6,358
  • 2
  • 12
  • 27
0
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 }
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Jan 21 '23 at 12:17