views:

61

answers:

4

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: 

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

nevermind, see this: http://stackoverflow.com/questions/1248302/javascript-object-size/1248343#1248343

pop850
He's not asking about arrays, or for a `sizeof`. He wants the number of mappings (key-value pairs) in an object.
Matthew Flaschen
`myObject.length` was my first thought, but yes, it only works for arrays.
MatrixFrog
A: 

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).

Matthew Flaschen
+1  A: 

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" :)

update
Failure example, as requested.

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

The count returned will be 3.

Nikita Rybak
The object is created with `myObject = {}` and then properties are added to it with `myObject[key] = value` where `key` is always a string. So I think that wouldn't be a concern in this case.
MatrixFrog
@MatrixFrog Oh, really? :) It doesn't matter what type is 'key'. I added example to clarify the point.
Nikita Rybak
btw, Anurag presses the same point in his answer (the comment in there)
Nikita Rybak
A: 

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();
Anurag
keys only returns the Enumerable, hasOwnProperty keys.
Matthew Flaschen
"TypeError: Object.keys is not a function". I guess I should have mentioned that I'm working on a Firefox extension, so if it doesn't work in Firefox then it's pretty much useless to me. Still, good information for the future. Thanks!
MatrixFrog
@Matthew - yes, that's why I said *defined on the object* and yes enumerability is important too, but that's what `for..in` will return as well, so I left it out of the discussion. @MatrixFrog - It should be coming in Firefox 4, and you can always defined a custom object type with a size method, as in the above example.
Anurag
@Matthew, to get all the properties, enumerable or not, we can now use in ES5 the [`Object.getOwnPropertyNames`](http://ecma262-5.com/ELS5_HTML.htm#Section_15.2.3.4) method.
CMS