1904

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');
3
  • 4
    Do you want the function to be a jQuery function or what does this have to do with jQuery? Commented Aug 11, 2010 at 5:01
  • 28
    That is actually a JavaScript object, associative arrays do not exist in JavaScript. Commented Aug 11, 2010 at 5:05
  • 3
    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). Commented Aug 11, 2010 at 5:12

3 Answers 3

3219

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.

Sign up to request clarification or add additional context in comments.

12 Comments

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?
Beware that IE8 may throw an exception when using delete in certain circumstances. See stackoverflow.com/questions/1073414/…
Does anyone know the runtime of this operation?
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.
If your linter is complaining about using the delete keyword, use: Reflect.deleteProperty(object1, 'property1');
|
298

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;
    }, {}
);

13 Comments

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.
@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.
object.property = undefined is legit. Worked perfectly! (upvoted)
Upvoted. A new immutable object is preferable in many cases.
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.
|
182

It's as easy as:

delete object.keyname;

or

delete object["keyname"];

4 Comments

If the key is generated number, and we does not know about it existing we can do next: const unknownKey = 100500; delete object[`${unknownKey}`];
How this answer is different from first one?
I just want to read @Sergii clarify this LOL
@Leprosy technically it's the same. But with unknown numbers as keys.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.