array.length and string indices

T

Thomas Mlynarczyk

Hi,

I have noticed, that for an array using string indices, the length property
does not seem to work.

a = new Array();
a['Name'] = 'Thomas';
a['Place'] = 'Home';

alert(a.length) will give 0.

Is there another way to get the number of elements in an array (or object)?

Greetings,
Thomas
 
M

Michael Winter

I have noticed, that for an array using string indices, the length
property does not seem to work.

a = new Array();
a['Name'] = 'Thomas';
a['Place'] = 'Home';

alert(a.length) will give 0.

Is there another way to get the number of elements in an array (or
object)?

As I understand it, you are not actually using an array. Arrays always use
numeric indices. What you are doing is creating new properties of that
array instance. This can be infered by using the dot notation:

a = new Array();
a['example'] = 'value';
alert( a['example'] + ' ' + a.example );

As you will see, you get the string, "value value". You could replace the
second line above with the following, and you would have exactly the same
result:

a.example = 'value';

This is why you can't get the array size using the length property: you're
using the behaviour of an object, not that of an array.

Mike
 
M

Martin Honnen

Thomas said:
I have noticed, that for an array using string indices, the length property
does not seem to work.

a = new Array();
a['Name'] = 'Thomas';
a['Place'] = 'Home';

alert(a.length) will give 0.

Is there another way to get the number of elements in an array (or object)?

Only the properties with names convertible to integers in the range
0..2^32-1 are considered "elements" of an array, everything else is just
a normal property and there is no built-in counter for the number
properties of objects (or arrays as some specialization of objects).
You could enumerate the properties of an object with
function countProps (object) {
var i = 0;
for (var property in object) {
i++;
}
return i;
}
var object = {
god: 'Kibo',
'-1': 'whatever'
}
var propCount = countProps(object);
alert(propCount);

var array = [0, 1];
array[-1] = 'whatever';
array.god = 'Kibo';
array[Math.pow(2, 32)] = 'whatelse';
propCount = countProps(array);
alert('array.length: ' + array.length + '; counted ' + propCount + '
properties');

but be aware that for..in leaves out properties marked as not enumerable
(for instance the length property of Array objects).
 
T

Thomas Mlynarczyk

Thanks to both of you for the reply. So there's no other way for me than to
loop through all the properties and increment a counter.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Thomas Mlynarczyk
I have noticed, that for an array using string indices, the length property
does not seem to work.

a = new Array();
a['Name'] = 'Thomas';
a['Place'] = 'Home';

alert(a.length) will give 0.

Is there another way to get the number of elements in an array (or object)?

Yes.

k = 0
for (j in a) k++
alert(k)
 
R

Richard Cornford

Thomas Mlynarczyk said:
Thanks to both of you for the reply. So there's no other way for me
than to loop through all the properties and increment a counter.

You are the one adding properties to the Array/object so you are also in
a position to keep count as you add them.

Richard.
 
G

Grant Wagner

Thomas said:
Thanks to both of you for the reply. So there's no other way for me than to
loop through all the properties and increment a counter.

Object.prototype.addProperties = function() {

if (typeof this.numberOfProperties == 'undefined') {
this.numberOfProperties = 0;
}

for (var i = 0; i < arguments.length; i+=2) {
this[arguments] = arguments[i + 1];
this.numberOfProperties++;
}
}

var a = {};
a.addProperties('one', 1, 'two', 2, 'three');
alert(a.numberOfProperties);

var b = [];
b.addProperties('four', 4, 'five', 5, 'six', 6);
alert(b.numberOfProperties);

You probably want some error checking to ensure you have an even number of
arguments passed to addProperties() (or not, if the number of arguments is odd,
you simply end up with the last property being undefined). You may also want to
create a method like count() to retrieve the number of properties rather then
accessing the counter property itself.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
T

Thomas 'PointedEars' Lahn

Grant said:
Thomas said:
[...] So there's no other way for me than to
loop through all the properties and increment a counter.

Object.prototype.addProperties = function() {

if (typeof this.numberOfProperties == 'undefined') {
this.numberOfProperties = 0;
}

for (var i = 0; i < arguments.length; i+=2) {
this[arguments] = arguments[i + 1];
this.numberOfProperties++;
}
}

var a = {};
a.addProperties('one', 1, 'two', 2, 'three');


The counter issue aside, a much better way is using a compound
structure, such as another object, as source for the new properties:

function addProperties(
/** @argument Object */ oSource,
/** @optional Object */ oParent)
{
if (!oParent)
{
oParent = this;
}

if (oParent)
{
for (var i in oSource)
{
oParent = oSource;
}
}
}

function addPrototypeProperties(
/** @argument Object */ oSource,
/** @optional Object */ oParent)
{
if (!oParent)
{
oParent = this;
}

if (oParent && typeof oParent.prototype != "undefined")
{
for (var i in oSource)
{
oParent.prototype = oSource;
}
}
}

addPrototypeProperties(
{
'addProperties' : addProperties,
'addPrototypeProperties': addPrototypeProperties
},
Object);


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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top