View Full Version : Resolved 3 JSON basic questions
It might look like elementary JSON issues but: does anyone know how to:
1. Detect if a JSON object is empty
2. Remove a property (along with its value) from a JSON object
- and related with the second question:
3. Clear the JSON object from all his properties (clear the object)
?
1) I would try something like:
Object.prototype.isEmpty = function() {
for (var prop in this) {
if (this.hasOwnProperty(prop)) return false;
}
return true;
};
2) There is a delete keyword:
var obj = { a: 1, b: 2 };
delete obj.a;
alert("a" in obj); // false
3) I would a combination of (1) and (2)
Object.prototype.clearProperties = function() {
for (var prop in this) {
if (this.hasOwnProperty(prop))
delete this[prop];
}
};
(Of course, why not simply discard the populated object and initialize a new one?)
Thanks. It should have been obvious for me from the beginning but, for a reason or another, I was initially handled the prototype in a wrong manner without any result.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.