Skip to content Skip to sidebar Skip to footer

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 = { checkb

Solution 1:

This is the bare minimum required:

functiondeleteFromObject(keyPart, obj){
    for (var k in obj){          // Loop through the objectif(~k.indexOf(keyPart)){ // If the current key contains the string we're looking fordelete 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

Solution 2:

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'
   };

constremoveProperty = dyProps => ({ [dyProps]: _, ...rest }) => rest;

const remove_random_property = removeProperty('random_property');
console.log(remove_random_property(myObject));

Solution 3:

You can use How to check if a string "StartsWith" another string?:

functiondeleteFromObject(keyToDelete, obj) {
    var l = keyToDelete.length;
    for (var key in obj)
        if (key.substr(0, l) == keyToDelete) // key begins with the keyToDeletedelete obj[key];
}

Solution 4:

If you're looking for a solution that doesn't mutate the original object, you can try something like this

constomit = (source = {}, omitKeys = []) => (
  Object.keys(source).reduce((output, key) => (
    omitKeys.includes(key) ? output : {...output, [key]: source[key]}
  ), {})
)

Tests

const original = {a:1, b:2, c:3, d:4, e:5}
console.log('original: ', JSON.stringify(original));

const modified = omit(original, ['b', 'd'])
console.log('modified: ', JSON.stringify(modified));
console.log('original: ', JSON.stringify(original));

// Will log: 
// original: {"a":1,"b":2,"c":3,"d":4,"e":5}
// modified: {"a":1,"c":3,"e":5}
// original: {"a":1,"b":2,"c":3,"d":4,"e":5}

This will create a new object and spread all properties of the source object into it, except those included in the exception list (omitKeys).

Post a Comment for "Object Properties Dynamic Delete"