Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Say I create an object thus:

var myJSONObject =
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};

What is the best way to remove the property 'regex'? i.e. I would like to end up with myJSONObject such that:

myJSONObject ==
        {"ircEvent": "PRIVMSG", "method": "newURI"};
share|improve this question

4 Answers

up vote 1152 down vote accepted

like this:

delete myJSONObject.regex;
// or,
delete myJSONObject['regex'];
// or,
var prop = "regex";
delete myJSONObject[prop];

Update: For anyone interested in reading more about it, kangax has written an incredibly in-depth blog post about the delete statement on his blog. Understanding delete. Highly recommended.

share|improve this answer
Is it possible to use associative array syntax with delete? Say i have the name of the property as a string i.e. 'regex'. – johnstok Oct 16 '08 at 11:04
3  
Checked, it also works with "delete myJSONObject['regex'];" See: developer.mozilla.org/en/Core_JavaScript_1.5_Reference/… – johnstok Oct 16 '08 at 11:06
Irritatingly MS fail to mention that in their own JScript documentation on MSDN. – AnthonyWJones Oct 16 '08 at 12:41
9  
An upshot of one of the observations at the "understanding delete" link above, is that, since you cannot necessarily delete a variable, but only object properties, you therefore cannot delete an object property "by reference" -- var value=obj['prop']; delete value //doesn't work – George Jempty Apr 16 '10 at 16:24
8  
@Pete no, it does remove it. Given: var x = {a : 'A', b : 'B'}; Compare: delete x.a; typeof x.a; /* "undefined" */ x.hasOwnProperty('a'); /* false */ to x.b = undefined; typeof x.b; /* "undefined" */; x.hasOwnProperty('b'); /* true */ – nickf Aug 10 '11 at 8:29
show 9 more comments
var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};

delete myJSONObject.regex;

alert ( myJSONObject.regex);

works in FF and IE and I think all others

share|improve this answer
Thanks for verifying – Mark Nov 18 '10 at 10:50

Wow, I didn't even KNOW there was a delete keyword! I guess you really do learn something new every day.

Probably useless, but I'll share the way I was going to go about this before learning of the delete keyword.

The way I thought of doing it was deletion by exclusion. Since it's an interesting way to do it, I will explain this workaround.

This, in fact, was the way I was going to go about doing it before I learned of the existence of this keyword. Here goes

DELETE=function(obj,prop){
    obj2=obj
    obj={}
    for(i in obj2){
        if(i != prop){
            obj[i]=obj2[i]
        }
    }
    return obj
}

Basically, this function just clears the object and rewrites it, excluding the one property specified, then returns it for example:

O={p1:'asdf',p2:'asdf',p3:'asdf'}
O=DELETE(O,'p1')
//O == {p2:'asdf',p3:'asdf'}, try it!


In hindsight, that was a terrible answer.

To make up for that, I will explain a situation where the "deletion by exclusion" method actually can be useful.

In arrays, unlike objects, using the delete keyword leaves null or undefined artifacts.

var array = [1,2,3,4];
delete array[2];
//Expected result --> [1,2,4]
//Actual result   --> [1,2,null,4]

This can be problematic if the array needs to be precise. For example, in a webapp that uses JSON serialized arrays to hold data in localStorage, and uses the indexes as well as the values within. This will result in "null" showing up, or if you use an if statement, completely skip numbers. Another problem is that JSON serialization also saves the null values, so this could rapidly result in clutter.

Instead, what you want to do is instead make a sort of garbage collection method

Array.prototype.remove = function(index){
    delete this[index];
    return this;
};
Array.prototype.clean = function(){
    var arr1 = this, arr2 = [];
    for(var a in arr1){
        if(arr1[a]&&arr1.hasOwnProperty(a)){
            arr2.push(arr1[a]);
        }
    }
    this.splice(0);
    for(var b in arr2){
        if(arr2.hasOwnProperty(b)){
            this.push(arr2[b]);
        }
    }
    return this;
};
var array = [1,2,3,4];
array.remove(2).clean();
// Result --> [1,2,4]


I hope this helped more than the last edit.

share|improve this answer
3  
This delete keyword, however, is much more convenient, lol – B1KMusic Sep 18 '12 at 0:59
4  
This approach doesn't modify the original object which might be still referenced elsewhere. This might or might not be a problem depending on how it's used but it's something to keep in mind. – Tamas Czinege Nov 7 '12 at 17:39
@DrJokepu Prior to learning about the delete method, what I did to prevent caching of the object was I first ran the DELETE function, then saved the object to localStorage with JSON, reloaded the page, and fetched the object again with JSON. The minimalist way in which I build my pages allowed the refresh to happen in a fraction of a second, so it isn't really noticed. However, the delete keyword is a lot more useful and convenient. – B1KMusic Nov 11 '12 at 0:14
I actually DID find a use for my deletion by exclusion method over the delete keyword. Apparently, when deleting from an array, it doesn't truly delete the index, the array's length stays the same, and the deleted property is set to null. So I used this instead, and it worked exactly as the delete keyword should have worked (but didn't). Just for information's sake, the application is for a tab system for a simple HTML5 notepad app I made for myself to have all my notes in one place. It uses an array which provides a list of all the tabs. – B1KMusic Dec 20 '12 at 23:35
3  
@B1KMusic Here's the way to delete an element from an Array: splice – wulftone Jan 25 at 20:20
show 4 more comments
myJSONObject.removeAttribute("PRIVMSG", 1);  //0 - case insensitive removing, 1 - vice versa, and 2 - returns it's value
share|improve this answer
1  
Please give any reference for this .removeAttribute() – Amol M Kulkarni Feb 8 at 10:19
9  
Whatever that is, it isn’t JavaScript. – minitech May 5 at 14:32

protected by Jason McCreary Oct 20 '11 at 18:27

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.