467

How can I remove an item from a JavaScript object?

Like this:

var test = {'red':'#FF0000', 'blue':'#0000FF'};
test.remove('blue');
2

1 Answer 1

805

var test = {'red':'#FF0000', 'blue':'#0000FF'};
delete test.blue; // or use => delete test['blue'];
console.log(test);

this deletes test.blue

11
  • 2
    This works great, you can check it here live: jsfiddle.net/b8whD/2 Commented Jun 9, 2011 at 15:21
  • 2
    What if we've an object like that : {0: 'red', 1:'green', 2:'blue'} delete test.0 will fail.
    – Hotgeart
    Commented Sep 15, 2015 at 16:41
  • 10
    @Hotgeart but delete test[0] will succeed. Although, if you are using naturally incrementing integers for your object keys maybe consider an array?
    – matchew
    Commented Sep 15, 2015 at 16:45
  • 7
    @Kermani No, delete has existed since ES1. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 17, 2016 at 16:42
  • 7
    @PatrickFinnigan It is funny, because I did not expect to see any easy good from ES1 :) Commented Sep 17, 2016 at 17:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.