Dynamic Delete Object Properties

I'm interested in an improved way to dynamically remove properties from a javascript object based on wildcards. First, 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 delete all the properties as '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 decision

My solution currently includes 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]; }; }); 

Question

Something about this seems very inappropriate to me, and if the object should be of a reasonable size, a very intensive process. Is there a better way to perform this operation?

+4
source share
2 answers

This is the minimum minimum:

 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 this is very close to the jQuery function you have.
(Although a little faster, given that it does not use jQuery and indexOf instead of match )

So what from ~ to indexOf ?

indexOf returns an integer value: -1 if the row is not found, and the index starting at 0 if it is found. (So ​​always a positive integer if found)
~ is the startling NOT , which inverts this output. Be that as it may, the inverted output of indexOf is what we need to indicate β€œfound” or β€œnot found”.

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

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

In addition, you can add a binary boolean ( !! ) before ~ to convert the true-ish or false-ish output to the true / false value, but this is not necessary in JavaScript.
Functionally ~string.indexOf('needle') and !!~string.indexOf('needle') are equal.


If you need a start key with a needle, replace:

 ~k.indexOf(keyPart) 

WITH

 k.indexOf(keyPart) === 0 
+7
source

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

 function deleteFromObject(keyToDelete, obj) { var l = keyToDelete.length; for (var key in obj) if (key.substr(0, l) == keyToDelete) // key begins with the keyToDelete delete obj[key]; } 
+2
source

All Articles