Friday, 6 January 2012

How to remove a property from a JavaScript object

JavaScript's 'delete' operator can be used for this.

The delete operator deletes a property of an object. It is part of JavaScript 1.2.

Syntax



delete expression

where expression should evaluate to a property reference, e.g.:

delete variableName

delete objectExpression.property

delete objectExpression["property"]

delete objectExpression[index]

Parameters



objectName
The name of an object.

property
The property to delete.

index
An integer representing the array index to delete.

Returns



Returns false only if the property exists and cannot be deleted. It returns true in all other cases.

Example




var myJSONObject = {};
myJSONObject.id = 101;

delete myJSONObject.id;
//OR
delete myJSONObject['id'];

No comments:

Post a Comment