How do I remove a key from a JavaScript object? [duplicate]

Asked 2023-09-21 08:10:23 View 808,702

Let's say we have an object with this format:

var thisIsObject= {
   'Cow' : 'Moo',
   'Cat' : 'Meow',
   'Dog' : 'Bark'
};

I wanted to do a function that removes by key:

removeFromObjectByKey('Cow');
  • Do you want the function to be a jQuery function or what does this have to do with jQuery? - anyone
  • That is actually a JavaScript object, associative arrays do not exist in JavaScript. - anyone
  • Yeah just some confusion with terminology I think, ie it's Javascript not Jquery, and it's an object not array (OP may come from other languages with associative arrays). - anyone

Answers

The delete operator allows you to remove a property from an object.

The following examples all do the same thing.

// Example 1
var key = "Cow";
delete thisIsObject[key]; 

// Example 2
delete thisIsObject["Cow"];

// Example 3
delete thisIsObject.Cow;

let animals = {
  'Cow': 'Moo',
  'Cat': 'Meow',
  'Dog': 'Bark'
};

delete animals.Cow;
delete animals['Dog'];

console.log(animals);

If you're interested, read Understanding Delete for an in-depth explanation.

Answered   2023-09-21 08:10:23

  • If you are looping over keys in an object, and delete them if they match a certain value, does this affect the key index while you are looping over it? - anyone
  • Beware that IE8 may throw an exception when using delete in certain circumstances. See stackoverflow.com/questions/1073414/… - anyone
  • Does anyone know the runtime of this operation? - anyone
  • In 10 years of JS-focused development in positions big and small, I have never once needed this before now or knew it existed. Thank you, kind sir, for this post. - anyone
  • If your linter is complaining about using the delete keyword, use: Reflect.deleteProperty(object1, 'property1'); - anyone

If you are using Underscore.js or Lodash, there is a function 'omit' that will do it.
http://underscorejs.org/#omit

var thisIsObject= {
    'Cow' : 'Moo',
    'Cat' : 'Meow',
    'Dog' : 'Bark'
};
_.omit(thisIsObject,'Cow'); //It will return a new object

=> {'Cat' : 'Meow', 'Dog' : 'Bark'}  //result

If you want to modify the current object, assign the returning object to the current object.

thisIsObject = _.omit(thisIsObject,'Cow');

With pure JavaScript, use:

delete thisIsObject['Cow'];

Another option with pure JavaScript.

thisIsObject = Object.keys(thisIsObject).filter(key =>
    key !== 'cow').reduce((obj, key) =>
    {
        obj[key] = thisIsObject[key];
        return obj;
    }, {}
);

Answered   2023-09-21 08:10:23

  • Downvoted. The thing is that both in underscore and lodash _.omit returns new object, does not modify the current one. So, this is slightly a different thing. - anyone
  • @shabunc The same with pure javascript. delete o.properrty does a lot more harm than good behind the scenes, as it changes o‘s hidden class and makes it a generic slow object. - anyone
  • object.property = undefined is legit. Worked perfectly! (upvoted) - anyone
  • Upvoted. A new immutable object is preferable in many cases. - anyone
  • I am sorry for the downvote, but thisIsObject.cow = undefined; is very misleading. Object.keys(thisIsObject) will still pick up 'cow'. So it's not really deleted. - anyone

It's as easy as:

delete object.keyname;

or

delete object["keyname"];

Answered   2023-09-21 08:10:23

  • If the key is generated number, and we does not know about it existing we can do next: const unknownKey = 100500; delete object[`${unknownKey}`]; - anyone
  • How this answer is different from first one? - anyone