Size of an object

C

Christoph Boget

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
 
J

Joost Diepenmaat

Christoph Boget said:
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.prototype that code will not do what
you probably want it to do. See Object.prototype.hasOwnProperty and
the DontEnum attribute in the ecmascript specs.

Joost.
 
V

VK

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.Dictionary');
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.
 
S

slebetman

If I had an array like so:
var bob = ['this', 'that', 'other'];
I can find out the size of the array by doing

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.Dictionary');
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
 
H

Henry

On Jun 20, 2:29 am, VK <[email protected]> wrote:

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) {
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('constructor');
d.remove('toString');
d.remove('valueOf');
d.remove('isPrototypeOf');

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

- and observe a significant flaw in that implementaion.
 
S

slebetman

But maybe best to steal one written by someone who understands the
issues.





^^^^
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('constructor');
d.remove('toString');
d.remove('valueOf');
d.remove('isPrototypeOf');

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.hasOwnProperty(key)) {
internalCount--;
delete internalHash[key];
}
}
 
T

Thomas 'PointedEars' Lahn

Henry said:
^^^^
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top