26

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.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • Dup: http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object – Félix Saparelli Oct 17 '11 at 03:17
  • Looping over an object is simple, a `for in` loop, but what do you mean by empty? How do you decide if a value is empty? Do you mean like an empty array (length === 0) or empty object (no properties)? – Zirak Oct 17 '11 at 03:19

2 Answers2

32

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.

Cristi Pufu
  • 9,002
  • 3
  • 37
  • 43
Zirak
  • 38,920
  • 13
  • 81
  • 92
  • 3
    So we could also use [Object.keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) in order to iterate through the object's own properties,right? Something like `Object.keys(myObj).forEach( function(ownKey){ delete myObj[ownKey] });` – Radu Jun 03 '16 at 10:12
3

You can use a for each loop to iterate through the object properties.

for ( var i in obj ) {
    if ( obj[i] === null ) {
        delete obj[i];
    }
}
Will
  • 19,661
  • 7
  • 47
  • 48