468

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

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

11 Comments

This works great, you can check it here live: jsfiddle.net/b8whD/2
What if we've an object like that : {0: 'red', 1:'green', 2:'blue'} delete test.0 will fail.
@Hotgeart but delete test[0] will succeed. Although, if you are using naturally incrementing integers for your object keys maybe consider an array?
@Kermani No, delete has existed since ES1. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@PatrickFinnigan It is funny, because I did not expect to see any easy good from ES1 :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.