By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

Javascript Array.push fix breaks for(var x in myarray)

P: n/a
In IE5 I am working with a lot of code that uses

Array.push()

as well as

for(var i in myArray) {}

Great - so I'm fixing the Array.prototype.push function for browsers
without it. That much works great. However as soon as I start
traversing arrays with for(... in ...) I end up with additional
elements at the end of the array.

A colleague said it was down to the added methods being seen as part of
the array , which to be sounds slightly strange!

Any ideas are welcome!

Thanks,

Andy.

Sep 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Andrew Phillipo" <an************@yahoo.co.uk> writes:
Array.push() .... for(var i in myArray) {}

Great - so I'm fixing the Array.prototype.push function for browsers
without it. That much works great. However as soon as I start
traversing arrays with for(... in ...) I end up with additional
elements at the end of the array.
Yes, the element "push".
A colleague said it was down to the added methods being seen as part of
the array , which to be sounds slightly strange!
Not really. The "for(..in..)" construct iterates over all properties
of an object (not marked as DONT_ENUM). It's not restricted to arrays,
and does nothing special for arrays, so it will find the "push" property
you have added.
Any ideas are welcome!


Use "for(var i = 0; i < arr.length; i++) {" instead of "for(var i in array){".

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 22 '05 #2

P: n/a
On 22/09/2005 17:46, Andrew Phillipo wrote:
In IE5 I am working with a lot of code that uses
[snip]
for(var i in myArray) {}
Well then, don't. Iterate over the array using the length property:

for(var i = 0, n = myArray.length; i < n; ++i) {
/* ... */
}

[snip]
[...] I end up with additional elements at the end of the array.


As you should. A for..in statement will walk over all enumerable
properties, which will include both user-defined properties (the array
elements) and methods.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Sep 22 '05 #3

P: n/a


Andrew Phillipo wrote:
In IE5 I am working with a lot of code that uses

Array.push()

as well as

for(var i in myArray) {}

Great - so I'm fixing the Array.prototype.push function for browsers
without it. That much works great. However as soon as I start
traversing arrays with for(... in ...) I end up with additional
elements at the end of the array.


No, for .. in does not traverse an array, it enumerates the properties
of any object, and enumeration might show methods as well as they are
simply function properties.

If you want to loop through an array in JavaScript use
for (var i = 0; i < myArray.length; i++) {
// access myArray[i]
}

Enumeration is useful for objects but even there you can of course run
into that trouble that someone for instance has added properties or
methods to Object.prototype. That is why you can find debates whether or
not someone writing a JavaScript library should add methods to
Object.prototype or not.

If you need enumeration you can distinguish methods with typeof checks e.g.

Object.prototype.someMethod = function () {}

var god = { name: 'Kibo', home: 'http://www.kibo.com/' };

var result = '';

for (var propertyName in god) {
result += propertyName + ': ' + (typeof god[propertyName]) + '\r\n';
}

alert(result);
In most implementations by now you can also use hasOwnProperty to find
out whether it is a property of the object itself or one in the
prototype chain:

Object.prototype.someMethod = function () {}

var god = { name: 'Kibo', home: 'http://www.kibo.com/' };

var result = '';

for (var propertyName in god) {
result += propertyName + ': ' + (typeof god[propertyName]) +
(god.hasOwnProperty ? '; hasOwnProperty("' + propertyName + '"): ' +
god.hasOwnProperty(propertyName) : '') + '\r\n';
}

alert(result);

But IE 5/Win with the JScript 5 it installs does not support
hasOwnProperty if recollection serves me right.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.