0

I have an

let obj = { a:'Nasa', b:'Zezo'}

Is there a way to make that object look like obj = { a:'Nasa' }

I am not looking at delete operator because the key still exists.

the delete operator leaves me with {a:'Nasa', b:null }

JS Lover
  • 622
  • 5
  • 16
  • Then you'll have to give a [mcve] with more detail about your environment, because that's not the behaviour I see in e.g. https://repl.it/languages/nodejs or read about in e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete. – jonrsharpe Apr 17 '19 at 12:05
  • 1
    `I am not looking at delete operator because the key still exists.` This is not true, so something else is causing you to not see the key disappear, as decribed in the duplicate answer. Then again, it's better to just create a new object obj2 containing only the `a` property of obj. That way the browser can still optimize obj and obj2 and you don't get issues later on when you have to check if obj still contains property `b` or not. – Shilly Apr 17 '19 at 12:07
  • Yes, I don't know why the key still exists. I will accept that as the correct answer. – JS Lover Apr 17 '19 at 14:34

1 Answers1

2

let obj = { a:'Nasa', b:'Zezo'}
console.log(obj);

delete obj.b;

console.log(obj);

You can use delete obj.b for deleting b property

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62