Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:40:10+00:00 2026-06-15T22:40:10+00:00

I am curious about an improved way to dynamically delete properties from a javascript

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:40:12+00:00Added an answer on June 15, 2026 at 10:40 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just curious about a particular scenario of NAT. Let's suppose we have 4 computers
I'm curious about there is any way to download library from server in running
Curious about running multiple xvfb displays: I have between 10-50 instances of a script
I'm curious about the correct way to architect an application that consists of the
I am a bit curious about how this works - if I have 5
I'm curious about what be the best way to model this for optimized performance...
Just curious about it... Cedric Beust created TestNG and I understand from an interview
Just curious about how jaxb works, I have a class annotated as follows: @XmlRootElement(name
Curious about things to consider to avoid any code breaks, etc to upgrade from
Just curious about SQL syntax. So if I have SELECT itemName as ItemName, substring(itemName,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.