1

This might be duplicated question, but I could not find any.

I wanna remove a key of an object.

Please check my fiddle first.

Remove a key of an object

delete obj.something;

When you check console, you'll be able to see the same object is printed in console in spite of using delete after first console.log.

Why this happening?

And what is the best idea of remove a key of an object?

Thanks in advance.

Canet Robern
  • 1,049
  • 2
  • 11
  • 28
  • 2
    `delete` *is* how you remove an object property, and in your fiddle the property *is* removed. I think you're getting confused by the console's behaviour: in some browsers when you `console.log()` an object it keeps a live link to the object and by the time you click the arrow to expand it the property has been removed. Try `console.log(JSON.stringify(obj))` as one way to log exactly the state of the object at that moment: https://jsfiddle.net/dvwgeb1o/2/. Also, note that your `newObj` variable in the fiddle doesn't refer to a new object, it is a second reference to the same object as `obj`. – nnnnnn Nov 08 '17 at 01:48
  • @nnnnnn You're right. I modified my fiddle like this. https://jsfiddle.net/Canet/dvwgeb1o/3/ I can see the value in new parameter is still alive. But in my project, this is not working. I put this value into new parameter before using delete, but this new parameter has nothing. I don't know why and this is the reason I wanna find other way. Thanks to your comment. – Canet Robern Nov 08 '17 at 02:04
  • 1
    Perhaps you should [edit] the question to show the non-working code, because the premise that `delete` doesn't work is just wrong, and the code shown in the question and fiddle works fine. – nnnnnn Nov 08 '17 at 02:08
  • @nnnnnn I'm afraid that my original code of these is in `$http` return function of Anugularjs project, so I cannot display in fiddle or pluker. The one certain thing is, the object which comes into this function has fine keys and new parameter still cannot get this removed value before using 'delete'. – Canet Robern Nov 08 '17 at 02:17
  • 1
    I don't understand why you can't show the relevant part of the project code, but without seeing that I don't think we can help. I think the problem you have is unrelated to `delete`. – nnnnnn Nov 08 '17 at 02:23
  • @nnnnnn Actually, the part of using 'delete' in my project is almost same like my fiddle. And I know that 'delete' function must work rightly, but I am not getting fine result so I want to find other way if there is a way to solve this. – Canet Robern Nov 08 '17 at 02:29
  • @nnnnnn In my project, this function is for deleting only this key and modifying parameter name from original object. So I think there are no problem in other place. :( – Canet Robern Nov 08 '17 at 02:32
  • 1
    Again, show your actual function in the question, and how you call it - if it's similar to the fiddle it will certainly fit in the question. Maybe we can spot something wrong with it that you haven't seen. "Some code that I won't show you doesn't work, how do I fix it?" is not an answerable question. – nnnnnn Nov 08 '17 at 02:35
  • @nnnnnn All right.. I know this question is too abstract to understand for other people. But there is nothing to show more. What I want is getting other way to remove key of object, not to fix this problem. – Canet Robern Nov 08 '17 at 04:22
  • 1
    As far as I know `delete` is the only way to remove a property from an existing object. (You can create a new object and then copy all the properties across except the one you want to delete, as in Prasanna's answer, then update your original variable to reference the new object, but that isn't actually a property deletion.) – nnnnnn Nov 08 '17 at 04:26
  • @nnnnnn It makes me sad that there's no other way. Then the only way to solve this problem is rechecking my code again I think. Thanks for your comments. These was real helpful for me. – Canet Robern Nov 08 '17 at 04:31

2 Answers2

1

You can try obj[key] = undefined;. It's roughly 100 times faster, according to: How do I remove a property from a JavaScript object?

  • That doesn't actually remove the property though, it just assigns a new value. So `for..in` loops, `Object.keys(obj)`, etc., will all still see the property. – nnnnnn Nov 08 '17 at 02:05
  • @nnnnnn Yeah. this is not for removing key, just make value undefined. – Canet Robern Nov 08 '17 at 02:07
1

you can use spread operators to remove keys.

let obj = {a:1, b:2, c:3}
const {a, ...restObj} = obj; // this will make restObj with deleted a key.

This method is especially useful when dealing with immutable types. This way you would not mutate the original object and still get an object with deleted key

Prasanna
  • 4,125
  • 18
  • 41
  • 1
    This method is definitely slower than the `delete` method. Because there are two assignments here, one for `a` and one for `...restObj`. But it is useful when you want to preserve your original object (immutability). That being said, YES!! , it is good for small objects – Prasanna Nov 08 '17 at 02:13
  • Then I cannot use this.. In my original code, real object is huge because it's from http call. But thank you for your suggestion. :) – Canet Robern Nov 08 '17 at 02:20