Size of an object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christoph Boget

    Size of an object

    If I had an array like so:

    var bob = ['this', 'that', 'other'];

    I can find out the size of the array by doing

    bob.length;

    Is there a comparable way to get the size of an object? So if I did:

    var bob = { 'this': 1, 'that' : 2, 'other': 3 };

    I could do something to find out that the "size" of the object is 3 -- the
    number of properties the object has? I know I can do this:

    var cnt = 0;
    for( var i in bob ) {
    cnt++;
    }

    but there's got to be a better/easier way? Or isn't there and I should just
    prototype the base Object class?

    thnx,
    Christoph

  • Joost Diepenmaat

    #2
    Re: Size of an object

    "Christoph Boget" <jcboget@yahoo. comwrites:
    I could do something to find out that the "size" of the object is 3 --
    the number of properties the object has? I know I can do this:
    >
    var cnt = 0;
    for( var i in bob ) {
    cnt++;
    }
    >
    but there's got to be a better/easier way? Or isn't there and I
    should just prototype the base Object class?
    There isn't a better way (except that this might not do what you
    expect, see below) and to be honest I've never had to find out just
    the number of properties of an object, while I iterate over the
    properties all the time.

    Oh, and if you *do* extend Object.prototyp e that code will not do what
    you probably want it to do. See Object.prototyp e.hasOwnPropert y and
    the DontEnum attribute in the ecmascript specs.

    Joost.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • VK

      #3
      Re: Size of an object

      On Jun 19, 8:56 pm, "Christoph Boget" <jcbo...@yahoo. comwrote:
      If I had an array like so:
      >
      var bob = ['this', 'that', 'other'];
      >
      I can find out the size of the array by doing
      >
      bob.length;
      >
      Is there a comparable way to get the size of an object?  So if I did:
      >
      var bob = { 'this': 1, 'that' : 2, 'other': 3 };
      >
      I could do something to find out that the "size" of the object is 3 -- the
      number of properties the object has?  I know I can do this:
      >
      var cnt = 0;
      for( var i in bob ) {
        cnt++;
      >
      }
      >
      but there's got to be a better/easier way?  Or isn't there and I shouldjust
      prototype the base Object class?
      There is not a better way - and you cannot prototype Object in any way
      other than adding some getCount function doing the same for-in loop.

      For whatever it worth mention, IE-specific Dictionary object (which is
      a real hash in Perl-like sense) does have Count property which is what
      you were thinking of.
      d = new ActiveXObject(' Scripting.Dicti onary');
      d.Add ('this', 1);
      d.Add ('that', 2);
      d.Add ('other', 3);
      window.alert(d. Count); // 3
      Of course this piece of knowledge is useless for open Web-wide
      solutions.

      Comment

      • slebetman

        #4
        Re: Size of an object

        On Jun 20, 2:29 am, VK <schools_r...@y ahoo.comwrote:
        On Jun 19, 8:56 pm, "Christoph Boget" <jcbo...@yahoo. comwrote:
        >
        >
        >
        If I had an array like so:
        >
        var bob = ['this', 'that', 'other'];
        >
        I can find out the size of the array by doing
        >
        bob.length;
        >
        Is there a comparable way to get the size of an object? So if I did:
        >
        var bob = { 'this': 1, 'that' : 2, 'other': 3 };
        >
        I could do something to find out that the "size" of the object is 3 -- the
        number of properties the object has? I know I can do this:
        >
        var cnt = 0;
        for( var i in bob ) {
        cnt++;
        >
        }
        >
        but there's got to be a better/easier way? Or isn't there and I should just
        prototype the base Object class?
        >
        There is not a better way - and you cannot prototype Object in any way
        other than adding some getCount function doing the same for-in loop.
        >
        For whatever it worth mention, IE-specific Dictionary object (which is
        a real hash in Perl-like sense) does have Count property which is what
        you were thinking of.
        d = new ActiveXObject(' Scripting.Dicti onary');
        d.Add ('this', 1);
        d.Add ('that', 2);
        d.Add ('other', 3);
        window.alert(d. Count); // 3
        Of course this piece of knowledge is useless for open Web-wide
        solutions.
        Or steal the idea and implement your own Dictionary:

        function Dict (obj) {
        var that = this;
        var internalHash = {};
        var internalCount = 0;

        if (obj) {
        for (var n in obj) {
        internalHash[n] = obj[n];
        internalCount++ ;
        }
        }

        that.set = function (key, value) {
        if (internalHash[key] === undefined) {
        internalCount++ ;
        }
        internalHash[key] = value;
        }

        that.get = function (key) {
        return internalHash[key];
        }

        that.remove = function (key) {
        if (internalHash[key] !== undefined) {
        internalCount--;
        delete internalHash[key];
        }
        }

        that.count = function () {
        return internalCount;
        }
        }

        var d = new Dict({
        this : 1,
        that : 2
        });
        d.set('other', 3);
        window.alert(d. count()); // 3

        Comment

        • Henry

          #5
          Re: Size of an object

          On Jun 20, 9:21 am, slebetman <slebet...@gmai l.comwrote:
          On Jun 20, 2:29 am, VK <schools_r...@y ahoo.comwrote:
          <snip>
          >Of course this piece of knowledge is useless for open
          >Web-wide solutions.
          >
          Or steal the idea and implement your own Dictionary:
          But maybe best to steal one written by someone who understands the
          issues.
          function Dict (obj) {
          var that = this;
          var internalHash = {};
          var internalCount = 0;
          >
          if (obj) {
          <snip>
          that.count = function () {
          return internalCount;
          }
          }
          >
          var d = new Dict({
          this : 1,
          ^^^^
          This, the code and the keyword in this context, is a syntax error.
          Only Identifiers, string literals and number literals may appear to
          the left of the colons in an object literal (though number literals
          don't work in some implementations (such as on Mac IE)).
          that : 2
          });
          d.set('other', 3);
          window.alert(d. count()); // 3
          Now add:-

          d.remove('const ructor');
          d.remove('toStr ing');
          d.remove('value Of');
          d.remove('isPro totypeOf');

          alert(d.count() ); // -1

          - and observe a significant flaw in that implementaion.

          Comment

          • slebetman

            #6
            Re: Size of an object

            On Jun 20, 6:24 pm, Henry <rcornf...@rain drop.co.ukwrote :
            On Jun 20, 9:21 am, slebetman <slebet...@gmai l.comwrote:
            >
            On Jun 20, 2:29 am, VK <schools_r...@y ahoo.comwrote:
            <snip>
            Of course this piece of knowledge is useless for open
            Web-wide solutions.
            >
            Or steal the idea and implement your own Dictionary:
            >
            But maybe best to steal one written by someone who understands the
            issues.
            >
            function Dict (obj) {
            var that = this;
            var internalHash = {};
            var internalCount = 0;
            >
            if (obj) {
            <snip>
            that.count = function () {
            return internalCount;
            }
            }
            >
            var d = new Dict({
            this : 1,
            >
            ^^^^
            This, the code and the keyword in this context, is a syntax error.
            Sorry, should have tested before posting :(
            should have been:

            var d = new Dict({
            'this' : 1,
            that : 2
            });
            >
            Now add:-
            >
            d.remove('const ructor');
            d.remove('toStr ing');
            d.remove('value Of');
            d.remove('isPro totypeOf');
            >
            alert(d.count() ); // -1
            >
            - and observe a significant flaw in that implementaion.
            Yeah, forgot about that. The remove method should be:

            that.remove = function (key) {
            if (internalHash.h asOwnProperty(k ey)) {
            internalCount--;
            delete internalHash[key];
            }
            }

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Size of an object

              Henry wrote:
              On Jun 20, 9:21 am, slebetman <slebet...@gmai l.comwrote:
              > var d = new Dict({
              > this : 1,
              ^^^^
              This, the code and the keyword in this context, is a syntax error.
              Only Identifiers, string literals and number literals may appear to
              the left of the colons in an object literal (though number literals
              don't work in some implementations (such as on Mac IE)).
              However, it should be noted that although `this' is listed as a reserved
              word in ECMAScript Ed. 3 Final, section 7.5.1, we are not looking at a
              syntax error in JavaScript 1.7 as supported by Firefox 2.0.0.14 and
              SeaMonkey 1.1.9, and JavaScript 1.8 as supported by Firefox 3.0, but merely
              an unfortunate choice of property name.


              PointedEars
              --
              Anyone who slaps a 'this page is best viewed with Browser X' label on
              a Web page appears to be yearning for the bad old days, before the Web,
              when you had very little chance of reading a document written on another
              computer, another word processor, or another network. -- Tim Berners-Lee

              Comment

              Working...