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

  • SEARCH
  • Home
  • 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 6701583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:57:05+00:00 2026-05-26T06:57:05+00:00

I have an object with various properties. The name of the object is a

  • 0

I have an object with various properties. The name of the object is a global variable but the properties are changed at runtime by methods. Some methods add properties to the object. I’d like to add a method that loops through this object and removes all of its properties that are either null or empty. I could check each property by specifying its name and checking its state but if I add properties later, I’d have to update this cleanup method too. How can I loop through the properties of an object without know the name of the properties first.

Thanks for your suggestions.

  • 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-05-26T06:57:06+00:00Added an answer on May 26, 2026 at 6:57 am

    Iteration over an object is simple – the for in loop:

    for (var key in object) {
        if (object.hasOwnProperty(key)) {
            //Now, object[key] is the current value
            if (object[key] === null || isEmpty(object[key]))
                delete object[key];
        }
    }
    

    isEmpty doesn’t exist, you have to define it or replace it with something more meaningful, I couldn’t understand what you meant by empty in your question.

    I use object.hasOwnProperty because objects inherit things from Object.prototype and possibly other places (arrays for example inherit from Array.prototype, which inherits from Object.prototype). So:

    object.toString; //function toString() { [native code] }
    

    But, object.toString actually refers to Object.prototype.toString – it isn’t really in your object, but when you type object.toString, the interpreter sees that there’s no object.toString, so it checks up the prototype chain until it finds it.

    hasOwnProperty checks if a key actually exists on an object:

    object.hasOwnProperty("toString"); //false
    object.foo = "bar";
    object.hasOwnProperty("foo"); //true
    

    Subscript access to objects is also simple:

    var object = {foo: "bar"};
    object.foo; //"bar"
    object["foo"]; //"bar"
    
    var key = "foo";
    object[key]; //"bar"
    

    Note that whatever is passed to the brackets gets converted to a string. So, for example, you can do this:

    object[Object.prototype.toString] = "magic";
    
    Object.keys(object); //["function toString() { [native code] }"]
    

    In case you’re wondering, Object.keys is an ES5 (EcmaScript 5) feature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of objects (IQueryable). Each object has various properties, some string
I want to create a Javascript class/object that allow me to have various method:
I've written an ATL/ActiveX object which exposes various properties and methods through its COM
I have an object that maintains a property bag of various properties. This is
I have an application which displays properties dialogs for various GUI-accessible objects. Because there
I have an object (Client * client) which starts multiple threads to handle various
I have a UITableView that displays various nsstrings from a custom object called a
I have a populated SqlCommand object containing the command text and parameters of various
I have various objects in application,and each has isvalid method to test if values
I have a Car class with various properties. I have a static Cars collection

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.