Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object properties dynamic delete

I am curious about an improved way to dynamically delete properties from a javascript object based on wildcards. Firstly, suppose I have the following object:

object =
{
    checkbox_description_1 : 'Chatoyant',
    checkbox_description_2 : 'Desultory',
    random_property : 'Firefly is a great program',
    checkbox_mood_1 : 'Efflorescent',
    checkbox_description_3 : 'Ephemeral'
}

Task

Now, the end result is to have removed all properties under the guise of 'checkbox_description' and leave the rest of the object intact as shown:

object =
{
    random_property : 'Firefly is a great program',
    checkbox_mood_1 : 'Efflorescent',
}

My solution

At present my solution involves jquery and the following code:

var strKeyToDelete = 'checkbox_description'

/* Start looping through the object */
$.each(object, function(strKey, strValue) {

    /* Check if the key starts with the wildcard key to delete */
    if(this.match("^"+strKey) == strKeyToDelete) {

        /* Kill... */
        delete object[strKey];
    };
});

Issue

Something about this seems very inelegant to me and if the object were to be of reasonable size very process intensive. Is there a better way of performing this operation?

like image 925
Symbal Avatar asked Dec 14 '12 08:12

Symbal


People also ask

How can we add or remove properties in object dynamically?

Adding/Removing Properties from an Object: For adding any property, one could either use object_name. property_name = value (or) object_name[“property_name”] = value. For deleting any property, one could easily use delete object_name. property_name (or) delete object_name[“property_name”].

How can we delete properties on objects?

Remove Property from an ObjectThe delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

How do you delete object items?

Answer: Use the delete Operator You can use the delete operator to completely remove the properties from the JavaScript object. Deleting is the only way to actually remove a property from an object.

Which operator can be used to delete properties from object?

The delete operator removes a property from an object. If the property's value is an object and there are no more references to the object, the object held by that property is eventually released automatically.


1 Answers

This is the bare minimum required:

function deleteFromObject(keyPart, obj){
    for (var k in obj){          // Loop through the object
        if(~k.indexOf(keyPart)){ // If the current key contains the string we're looking for
            delete obj[k];       // Delete obj[key];
        }
    }
}

var myObject = {
    checkbox_description_1 : 'Chatoyant',
    checkbox_description_2 : 'Desultory',
    random_property : 'Firefly is a great program',
    checkbox_mood_1 : 'Efflorescent',
    checkbox_description_3 : 'Ephemeral'
};
deleteFromObject('checkbox_description', myObject);
console.log(myObject);
// myObject is now: {random_property: "Firefly is a great program", checkbox_mood_1: "Efflorescent"};

So that's pretty close to the jQuery function you have.
(Though a little faster, considering it doesn't use jQuery, and indexOf instead of match)

So, what's with the ~ before indexOf?

indexOf returns a integer value: -1 if the string is not found, and a index, starting from 0, if it is found. (So always a positive integer if found)
~ is a bitwise NOT, that inverts this output. As it happens to be, the inverted output of indexOf is just what we need to indicate "found" or "not found".

~-1 becomes 0, a false-ish value.
~x, where x is 0 or postitive, becomes -(x+1), a true-ish value.

This way, ~string.indexOf('needle') acts like string.contains('needle'), a function that we don't have in JavaScript.

Additionally, you could add a double boolean not (!!) in front of the ~, to convert the true-ish or false-ish output to a real true / false, but that's not necessary in JavaScript.
Functionally, ~string.indexOf('needle') and !!~string.indexOf('needle') are equal.


In case you specifically need the key to begin with the needle, replace the:

~k.indexOf(keyPart)

With:

k.indexOf(keyPart) === 0
like image 195
Cerbrus Avatar answered Sep 21 '22 06:09

Cerbrus