473,805 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for(i in array)

Hi,

For objects (non-arrays) I can use for(i in object) to loop through all its
members. For arrays, this does not always seem to work, and I am forced to
use for(i=0; i<array.length; i++) instead. Is there a way to make an array
accessible using the for-in loop? During my experiments I found that if the
array is a member of an object, but not the first one, the for-in will
work... seems a strange behaviour to me, so I'd be grateful if someone could
shed some light on this for me.

Greetings,
Thomas
Jul 23 '05 #1
10 1552
Thomas Mlynarczyk wrote:
For objects (non-arrays) I can use for(i in object)
to loop through all its members.
To loop through the _enumerable_ members.
For arrays, this does not always seem to work,
I have seen no evidence of any ECMAScript implementations exhibiting
incorrect (unspecified) behaviour with for-in loos (except Netscape 4,
with respect to the Type of the property name value, only).
and I am forced to use
for(i=0; i<array.length; i++) instead.
If the members of an Array object that have property names that
sufficiently resemble 32 bit unsigned integers are the subject of
interest (as is almost always the case) then a loop incrementing (or
decrementing) a number and limited by the Array's - length - property is
the correct/reliable approach.
Is there a way to make an array
accessible using the for-in loop?
Arrays are objects and will exhibit the (ECMA 262) specified behaviour
when used as the subject of a for-in loop.
During my experiments I found that if the array
is a member of an object, but not the first one, the for-in
will work... seems a strange behaviour to me, so I'd be
grateful if someone could shed some light on this for me.


In the above you have declared unshown code as 'working' or 'not
working', without stating the criteria for either. As, in a strictly
technical sense, all computer code 'works' (the computer mindlessly
follows the instructions provided regardless of the outcome), it is
almost always necessarily to explain what intended/expected behaviour
corresponds with your definition of 'works', and provide that
demonstrates how you are achieving 'not working'.

Richard.
Jul 23 '05 #2
Also sprach Richard Cornford:
Thomas Mlynarczyk wrote:
For objects (non-arrays) I can use for(i in object)
to loop through all its members.
To loop through the _enumerable_ members.


Is there a website which explains this issue in more detail?
Arrays are objects and will exhibit the (ECMA 262) specified behaviour
when used as the subject of a for-in loop.
Does this mean if I access an array using for-in it should work and if it
doesn't there must be a bug in my script?
In the above you have declared unshown code as 'working' or 'not
working', without stating the criteria for either. As, in a strictly
technical sense, all computer code 'works' (the computer mindlessly
follows the instructions provided regardless of the outcome), it is
almost always necessarily to explain what intended/expected behaviour
corresponds with your definition of 'works', and provide that
demonstrates how you are achieving 'not working'.


Strangely, I was not able to reproduce the behaviour in a minimal test case,
so it seems that it is indeed a bug in my original script, even though I
cannot see how it could be possible. My script has a function which takes an
object as argument (usually created "on the fly" in the function call
statement). It loops through it using for-in and displays the member names
and their values. If a value is an object (or array), it recursively loops
through its members to display them as well.

Jul 23 '05 #3
Thomas Mlynarczyk wrote:
Also sprach Richard Cornford:


[snip]
To loop through the _enumerable_ members.


Is there a website which explains this issue in more detail?


Not to my knowledge, though it is explained in the ECMAScript
specification[1] (8.6.1). In any case, it's quite simple.

All object properties have a set of attributes associated with them.
These attributes are ReadOnly, DontEnum, DontDelete, and Internal. The
specification gives standardised properties preset attributes -
namely, most (perhaps all? I'm not going to look right now :D) methods
and properties are marked DontEnum. Host objects may, or may not,
define attributes for their properties. It's up to the implementor.

Whilst you can access DontEnum properties, you can only do so
directly. The for..in statement will not discover these properties.

Unfortunately, user code cannot manipulate property attributes. Any
user-created properties will have no attributes set.
Arrays are objects and will exhibit the (ECMA 262) specified behaviour
when used as the subject of a for-in loop.


Does this mean if I access an array using for-in it should work and if it
doesn't there must be a bug in my script?


There might be a bug in the ECMAScript implementation, but yes, it
could be your code.

Hope that helps,
Mike

[1]
<URL:http://www.ecma-international.o rg/publications/files/ecma-st/ECMA-262.pdf>

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Thomas Mlynarczyk wrote:
Also sprach Richard Cornford:
Thomas Mlynarczyk wrote:
For objects (non-arrays) I can use for(i in object)
to loop through all its members.


To loop through the _enumerable_ members.


Is there a website which explains this issue in more detail?


What issue? ECMA 262 says that the for-in statement should behave in a
particular way and language implementations behave in that way.
Arrays are objects and will exhibit the (ECMA 262) specified
behaviour when used as the subject of a for-in loop.


Does this mean if I access an array using for-in it should
work and if it doesn't there must be a bug in my script?


How are you defining 'work' and 'doesn't (work)'?

<snip>
..., it is almost always necessarily to explain
what intended/expected behaviour corresponds
with your definition of 'works', and provide that ^^^^^^^^^^^^
That should have read "provide code that ...".
demonstrates how you are achieving 'not working'.


Strangely, I was not able to reproduce the behaviour
in a minimal test case, so it seems that it is indeed
a bug in my original script, even though I cannot
see how it could be possible. ...

<snip>

And nobody else can _see_ it either.

However, when you cannot reproduce a problem by isolating the suspected
cause you have eliminated that possibility as the cause of the problem
and know that it is time to look elsewhere.

Richard.
Jul 23 '05 #5
Also sprach Michael Winter:
All object properties have a set of attributes associated with them.
These attributes are ReadOnly, DontEnum, DontDelete, and Internal. The
specification gives standardised properties preset attributes -
namely, most (perhaps all? I'm not going to look right now :D) methods
and properties are marked DontEnum. Host objects may, or may not,
define attributes for their properties. It's up to the implementor.
Whilst you can access DontEnum properties, you can only do so
directly. The for..in statement will not discover these properties.
Unfortunately, user code cannot manipulate property attributes. Any
user-created properties will have no attributes set.


Thanks for the explanation. I had noticed, that, when looping recursively
through a DOM object (like document), the for-in loop seems to do several
rounds, but no data is retrieved. Moreover, in some browsers I get an error
like "Could not access...". Is this likely to be a bug in my script?
Jul 23 '05 #6
"Thomas Mlynarczyk" <bl************ *@hotmail.com> wrote in message
news:d0******** *****@news.t-online.com...
Also sprach Michael Winter:
All object properties have a set of attributes associated with them.
These attributes are ReadOnly, DontEnum, DontDelete, and Internal.
The
specification gives standardised properties preset attributes -
namely, most (perhaps all? I'm not going to look right now :D)
methods
and properties are marked DontEnum. Host objects may, or may not,
define attributes for their properties. It's up to the implementor.
Whilst you can access DontEnum properties, you can only do so
directly. The for..in statement will not discover these properties.
Unfortunately, user code cannot manipulate property attributes. Any
user-created properties will have no attributes set.


Thanks for the explanation. I had noticed, that, when looping
recursively
through a DOM object (like document), the for-in loop seems to do
several
rounds, but no data is retrieved. Moreover, in some browsers I get an
error
like "Could not access...". Is this likely to be a bug in my script?


No, it is not a bug in your script. Some properties of some host objects
in some browsers are not accessible.

For example, the following works as expected in Internet Explorer:

for (var prop in navigator)
{
document.write(
prop + ' = ' + navigator[prop] +
' (type: ' + typeof navigator[prop] + ')<br>'
);
}

However:

for (var prop in navigator.plugi ns)
{
document.write(
prop + ' = ' + navigator.plugi ns[prop] +
' (type: ' + typeof navigator.plugi ns[prop] + ')<br>'
);
}

Produces an "Object doesn't support this property or method" error, even
though navigator.plugi ns reports typeof "object" when enumerating the
navigator object.

As a result, it is not a good idea to recursively enumerate the
enumerable properties of host objects (and their child objects), since
you can not guarantee it will not produce an unrecoverable error.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #7
Also sprach Grant Wagner:
As a result, it is not a good idea to recursively enumerate the
enumerable properties of host objects (and their child objects), since
you can not guarantee it will not produce an unrecoverable error.


So there are objects which don't like to be asked about their properties...
And no way to find out before one tries accessing them? Or could I somehow
use try-catch to skip such objects?
Jul 23 '05 #8
>>As a result, it is not a good idea to recursively enumerate the
enumerable properties of host objects (and their child objects), since
you can not guarantee it will not produce an unrecoverable error.
So there are objects which don't like to be asked about their properties...
And no way to find out before one tries accessing them? Or could I somehow
use try-catch to skip such objects?


You can try, but that will produce syntax errors at load time on old
browsers.
Jul 23 '05 #9
>>> As a result, it is not a good idea to recursively enumerate the
enumerable properties of host objects (and their child objects), since
you can not guarantee it will not produce an unrecoverable error.


So there are objects which don't like to be asked about their
properties...
And no way to find out before one tries accessing them? Or could I
somehow
use try-catch to skip such objects?

You can try, but that will produce syntax errors at load time on old
browsers.


I have had some success with the isAlien function, but I haven't tested
it on all browsers.

function isAlien(a) {
return isObject(a) && typeof a.constructor != 'function';
}
Jul 23 '05 #10

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

Similar topics

14
1452
by: Qwert | last post by:
Hello, if you have an array with some class as element type and a same sized array with some structure as element type, and that class and structure have the same fields (for example 10 integers), is it correct to assume that the array with the classes uses less memory (than the array with structures) before the objects are initialized (X bytes per object) and uses more memory after all the objects are initialized (same fields + X bytes...
6
1563
by: Fla | last post by:
Hi! I would like to use Compression namespace for Array, i.e. use .NET native Compression for compress a String, or an Array of Integer whose elements are returned values of AscW for each char of the String. I tried with the code available @ http://msdn2.microsoft.com/en-us/library/system.io.compression.gzipstream(VS.80).aspx but with no success 'cause I obtained ms.lenght buffer.length. This is my source code for CompressData:
3
1312
by: fdu.xiaojf | last post by:
Hi, It seems that an array acts like an list very much, except it doesn't have a method sort. Regards,
3
14596
by: uche | last post by:
Please give me some feed back on this issue: Here is the complier error: hexdmp.cpp: In function `void output(unsigned char, int, bool&)': hexdmp.cpp:133: error: invalid types `unsigned char' for array subscript hexdmp.cpp:146: error: invalid types `unsigned char' for array subscript hexdmp.cpp:146: error: invalid types `unsigned char' for array
9
2164
by: barcaroller | last post by:
Can variables be used for array size in C++? I know that in the past, I could not do the following: foo (int x) { type arr; } I have recently seen code that does exactly that. Is it right?
3
2419
by: Sebastian Deutsch | last post by:
Hello, is there a shorter syntax for array beside array("one", "two"), something link {"one", "two"}? cheers sebastian
45
7402
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
8
1279
by: sloan | last post by:
string array = new string {"A","B","C"}; Given the above data, I'm trying to find C# code which will produce A,A B,A
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10609
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10360
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.