Tutorial :Determine how many fields a Javascript object has


I have a Javascript object that I'm trying to use as a "hashmap". The keys are always strings, so I don't think I need anything as sophisticated as what's described in this SO question. (I also don't expect the number of keys to go above about 10 so I'm not particularly concerned with lookups being O(n) vs. O(log n) etc.)

The only functionality I want that built-in Javascript objects don't seem to have, is a quick way to figure out the number of key/value pairs in the object, like what Java's Map.size returns. Of course, you could just do something like:

function getObjectSize(myObject) {    var count=0    for (var key in myObject)      count++    return count  }  

but that seems kind of hacky and roundabout. Is there a "right way" to get the number of fields in the object?


A correction: you need to check myObject.hasOwnProperty(key) in each iteration, because there're can be inherited attributes. For example, if you do this before loop Object.prototype.test = 'test', test will aslo be counted.

And talking about your question: you can just define a helper function, if speed doesn't matter. After all, we define helpers for trim function and other simple things. A lot of javascript is "kind of hacky and roundabout" :)

Failure example, as requested.

Object.prototype.test = 'test';  var x = {};  x['a'] = 1;  x['b'] = 2;  

The count returned will be 3.


There is an easier way spec'd in ECMAScript 5.

Object.keys(..) returns an array of all keys defined on the object. Length can be called on that. Try in Chrome:

Object.keys({a: 1, b: 2}).length; // 2  

Note that all objects are basically key/value pairs in JavaScript, and they are also very extensible. You could extend the Object.prototype with a size method and get the count there. However, a much better solution is to create a HashMap type interface or use one of the many existing implementations out there, and define size on it. Here's one tiny implementation:

function HashMap() {}    HashMap.prototype.put = function(key, value) {      this[key] = value;  };    HashMap.prototype.get = function(key) {      if(typeof this[key] == 'undefined') {          throw new ReferenceError("key is undefined");      }      return this[key];  };    HashMap.prototype.size = function() {      var count = 0;        for(var prop in this) {          // hasOwnProperty check is important because           // we don't want to count properties on the prototype chain          // such as "get", "put", "size", or others.          if(this.hasOwnProperty(prop) {              count++;          }      }        return count;  };  

Use as (example):

var map = new HashMap();  map.put(someKey, someValue);  map.size();  


you could also just do myObject.length (in arrays)

nevermind, see this: JavaScript object size


That's all you can do. Clearly, JavaScript objects are not designed for this. And this will only give you the number of Enumerable properties. Try getObjectSize(Math).

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »