how to remove a key from a JavaScript object?

+4 votes
431 views
asked Dec 13, 2012 in java script by codeg (2,600 points)

 

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

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

i wanted to do a function that removes by key:

removeFromArrayByKey('Cow');

1 Answer

0 votes
answered Dec 13, 2012 by sahana (2,250 points)

probably this solve your problem

 

function removeFromArrayByKey(key) {
  delete thisIsArray[key];
}

But I would probably keep it simple and not use the function.

delete thisIsArray[key];
...