Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    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)

    ?
    Last edited by Kor; 10-17-2008 at 08:26 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    1) I would try something like:
    Code:
    Object.prototype.isEmpty = function() {
        for (var prop in this) {
            if (this.hasOwnProperty(prop)) return false;
        }
        return true;
    };
    2) There is a delete keyword:
    Code:
    var obj = { a: 1, b: 2 };
    delete obj.a;
    alert("a" in obj); // false
    3) I would a combination of (1) and (2)
    Code:
    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?)

  • Users who have thanked jkd for this post:

    Kor (10-17-2008)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •