How to remove a property from a javascript object
49
Say I create an object thus:

What is the best way to remove the property 'regex'? i.e. I would like to end up with myJSONObject such that:
improve this question | comment
Kayleigh Beer Created at: 2013-11-13 17:07:06 UTC By Kayleigh Beer
3 Answers
0
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.
0
var myJSONObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};

delete myJSONObject.regex;

alert ( myJSONObject.regex);


works in FF and IE and I think all others
0
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.
Your Answer