Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're not using an array as an array anymore in your example. Arrays in JS are (strictly speaking) only a bunch of stuff like this:</p> <p><code>['a', 'b', 'c']</code></p> <p>What you've done is started to use the empty array as an object.. the [ ] simple accesses a property via a string/variable... so</p> <pre><code>var obj = {test: 'blah'} obj['test'] //is 'blah' obj.test //is also blah </code></pre> <p>Generally the [ ] notation is used when you're going to access properties or methods via a variable or if the property or method is not something that is 'safe'.. for instance 'int'.</p> <p>So, change what you have into an object and then step through the keys with a 'for in' loop. Yes, you'll still have to check on each one to see if it hasOwnProperty (or use a jquery/underscore 'each'), but at least you'll be using the right tool for the job :) Or if you just want the count of keys, underscore.js has a nice little _.keys method.. so _.keys(obj).length would give you the number of keys. Pretty sure it's just local keys, not prototype ones..</p> <p>Edit: as a re-read and hopefully better to the point answer, there is no property in JS that I'm aware of where you can simply get a count of the keys. If it was just a particular thing you were working with and not 'all object's, you could do something like..</p> <pre><code>var specialObject = function () {this.keys = 0;}; specialObject.prototype.set = function (key, val) { if(!this[key]) { this.keys++; } this[key] = val; }; var so = new specialObject(); so.set('test', 'blah'); so.keys //would be 1 so.test //would be 'blah' </code></pre> <p>and then just always use 'set'. You'd want to make an 'unset' and possibly a 'get' method as well.</p> <p>Hopefully this all makes sense.. it's a bit rambly.</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.
    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