Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way to do this, with excellent performance and compatibility with both old and new browsers, is to include either <a href="http://lodash.com/" rel="noreferrer">Lo-Dash</a> or <a href="http://underscorejs.org/" rel="noreferrer">Underscore</a> in your page.</p> <p>Then you can use either <code>_.size(object)</code> or <code>_.keys(object).length</code></p> <p>For your <code>obj.Data</code>, you could test this with:</p> <pre><code>console.log( _.size(obj.Data) ); </code></pre> <p>or:</p> <pre><code>console.log( _.keys(obj.Data).length ); </code></pre> <p>Lo-Dash and Underscore are both excellent libraries; you would find either one very useful in your code. (They are rather similar to each other; Lo-Dash is a newer version with some advantanges.)</p> <p>Alternatively, you could include this function in your code, which simply loops through the object's properties and counts them:</p> <pre><code>function ObjectLength( object ) { var length = 0; for( var key in object ) { if( object.hasOwnProperty(key) ) { ++length; } } return length; }; </code></pre> <p>You can test this with:</p> <pre><code>console.log( ObjectLength(obj.Data) ); </code></pre> <p>That code is not as fast as it could be in modern browsers, though. For a version that's much faster in modern browsers and still works in old ones, you can use:</p> <pre><code>function ObjectLength_Modern( object ) { return Object.keys(object).length; } function ObjectLength_Legacy( object ) { var length = 0; for( var key in object ) { if( object.hasOwnProperty(key) ) { ++length; } } return length; } var ObjectLength = Object.keys ? ObjectLength_Modern : ObjectLength_Legacy; </code></pre> <p>and as before, test it with:</p> <pre><code>console.log( ObjectLength(obj.Data) ); </code></pre> <p>This code uses <code>Object.keys(object).length</code> in modern browsers and falls back to counting in a loop for old browsers.</p> <p>But if you're going to all this work, I would recommend using Lo-Dash or Underscore instead and get all the benefits those libraries offer.</p> <p>I set up a <a href="http://jsperf.com/counting-object-properties" rel="noreferrer">jsPerf that compares the speed of these various approaches</a>. Please run it in any browsers you have handy to add to the tests.</p> <p>Thanks to <a href="https://stackoverflow.com/users/1491895/barmar">Barmar</a> for <a href="https://stackoverflow.com/a/16976940/1202830">suggesting <code>Object.keys</code> for newer browsers</a> in his answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload