Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript looping and deleting object properties

Tags:

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.

like image 259
frenchie Avatar asked Oct 17 '11 03:10

frenchie


People also ask

How do we delete the object property in JavaScript?

The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

Which JavaScript loop loops through the properties of an object?

Description. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its prototype chain (properties of nearer prototypes take precedence over those of prototypes further away from the object in its prototype chain).

Which operator can be used to delete properties from object?

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.

How do I remove a property from an object name?

The delete operator is used to remove these keys, more commonly known as object properties, one at a time. The delete operator does not directly free memory, and it differs from simply assigning the value of null or undefined to a property, in that the property itself is removed from the object.


2 Answers

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.

like image 182
Zirak Avatar answered Nov 28 '22 09:11

Zirak


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];
    }
}
like image 37
Will Avatar answered Nov 28 '22 09:11

Will