473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Size of an object

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

Jun 27 '08 #1
6 14093
"Christoph Boget" <jc*****@yahoo.comwrites:
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.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #2
VK
On Jun 19, 8:56*pm, "Christoph Boget" <jcbo...@yahoo.comwrote:
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.
Jun 27 '08 #3
On Jun 20, 2:29 am, VK <schools_r...@yahoo.comwrote:
On Jun 19, 8:56 pm, "Christoph Boget" <jcbo...@yahoo.comwrote:
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?

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

Jun 27 '08 #4
On Jun 20, 9:21 am, slebetman <slebet...@gmail.comwrote:
On Jun 20, 2:29 am, VK <schools_r...@yahoo.comwrote:
<snip>
>Of course this piece of knowledge is useless for open
Web-wide solutions.

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) {
<snip>
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.
Jun 27 '08 #5
On Jun 20, 6:24 pm, Henry <rcornf...@raindrop.co.ukwrote:
On Jun 20, 9:21 am, slebetman <slebet...@gmail.comwrote:
On Jun 20, 2:29 am, VK <schools_r...@yahoo.comwrote:
<snip>
Of course this piece of knowledge is useless for open
Web-wide solutions.
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) {
<snip>
that.count = function () {
return internalCount;
}
}
var d = new Dict({
this : 1,

^^^^
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];
}
}
Jun 27 '08 #6
Henry wrote:
On Jun 20, 9:21 am, slebetman <slebet...@gmail.comwrote:
> 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)).
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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: grayFalcon | last post by:
Hello! I've got a small problem here. I'm trying to write some code that would generate a drop-down menue for me, where I'd just need to enter the menu- and submenu items into an array. The...
22
by: ajay | last post by:
Why would a new of object be created without assigning it to any of variable? new A; ??? tx
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
5
by: Greg | last post by:
Hi, I have been unable to resolve this error message: Object Ref not set to an instance of an object. The issue is that I cannot determine which object reference is causing the problem. ...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
5
by: Pohihihi | last post by:
Why can't the following work when it has a set property defined? this.textBox1.Size.Width = 25; I get error Cannot modify the return value of 'System.Windows.Forms.Control.Size' because it is...
8
by: filox | last post by:
is there a way to find out the size of an object in Python? e.g., how could i get the size of a list or a tuple? -- You're never too young to have a Vietnam flashback
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
GaryTexmo
by: GaryTexmo | last post by:
Hi all, I'm going to describe a method of scaling your objects so they fit correctly on your screen no matter what size of canvas you're drawing them on. This is going to be a fairly simple,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.