views:

591

answers:

5

I have this javascript code working in firefox, chrome, and safari

for (idx in all_auction_ids){
    alert(all_auction_ids[idx]);
};

for the above, instead of getting the values in all_auction_ids, the first value I get is text of type function that looks like a for loop!

But if I run the code below, it works fine.

for (idx=0;idx<all_auction_ids.length;idx=idx+1){
    alert(all_auction_ids[idx]);
};

edit: updates

I did some debugging and found out that, adding Orbited and stomp.js is probably doing something with the array!

for now i am using Tracker1's suggestion jquery's $.each.

more info: http://groups.google.com/group/orbited-users/browse_thread/thread/7fd658cfb166e9fa

array with the problem http://bayimg.com/fAnhaAaBb

array without the problem http://bayimg.com/FaNhEAabb

+4  A: 

JavaScript's for/in construct is traditionally for iterating over object member names, not array indices. The more forward-thinking browsers have added features like hidden properties to help cases like Array enumerate in the way you would expect, but IE stilll does it the old-school way and gives you Object members like the 'toString' method when you use for/in over an Array.

The indexed-for is still the canonical JavaScript array loop. (Although you probably mean 'for (var idx=...', and 'idx++' is more common.)

bobince
I agree that the "for(var i = 0..." method is preferable for an indexed array, but IE has always supported the "for(var i in..." iterator because an Array object is just an object with an auto-increment feature after all.
Prestaul
yeah!, IE should support for in , this is weird right?
mark
@kevin: Not "should", DOES! There is something else going on here and I don't think we've been given enough detail yet...
Prestaul
@bobince, when was the last time you saw IE return 'toString' using for/in on an Array? I don't think I've ever seen this, going back to IE5...
Prestaul
Hmm, you're right, seems toString has been gone for a while. However there is also the chance something could have added to Array.prototype... actually if you're running one of those JavaScript frameworks that just loves spraying methods into prototypes it's a certainty.
bobince
@bobince, that is certainly true. @mark (@kevin?), are you using any javascript libraries (e.g. jQuery, Prototype.js, Mootools, something homegrown)?
Prestaul
+1  A: 

I agree with @bibince that you probably should be using the "for(var i = 0..." syntax, but there is no reason that the syntax you chose should not work unless you have done something strange in your creation of all_auction_ids. How are you initializing your array?

Arrays in JavaScript are just objects with a special auto-incrementing feature, but in reality they are not much different that an anonymous object. Try this in Firebug:

var a = ['a','b','c'];
a.d = 'd';
for(var i in a) console.log(i, a[i]);

or paste this into your address bar in IE and hit enter:

javascript:var a = ['a']; a.d = 'd'; for(var i in a) alert(a[i]); alert(a.length);

EDIT:

I doubt this is your problem, but do you have the same problem if you use:

var all_auction_ids = [];

rather than

var all_auction_ids = new Array();

If that doesn't help then could you post a little more of your code to give us a better idea of how you are populating all_auction_ids?

Prestaul
Prestaul:I have this problem only in IE (both 7 and 8) not in firefox.all_auction_ids = new Array();and i add data to it this way.all_auction_ids.push(id);it works well in safari/ffox
mark
@kevin: I've updated my post. I think that I would start by changing the declaration to var all_auction_ids = []; and removing the semi-colon after the for block. That probably isn't your problem but we might need more detail to figure it out.
Prestaul
Also, you might add a "var"... i.e. "for(var idx in..."
Prestaul
+2  A: 

It's worth noting that some libraries such as prototype.js extend Array, so that they have additional properties beyond the internal indexes. This breaks for x in y notation beyond, as other mentioned, that IE will iterate properties. for i=0...i++ is preferred.

Also worth noting is jQuery, prototype and others offer a .each(fn) notation that I actually prefer.

Tracker1
A: 

This topic on the YUI blog is germane to your problem.

Ishmael
A: 

I've been having similar problems lately creating "select all / clear all" buttons for lists of checkboxes. In Firefox and Chrome they work fine, but in IE7/8 they don't. I'm not using any frameworks or external libraries, all the JavaScript is my own, it's fairly straightforward stuff, and there isn't much of it. I build the array of input elements using getElementsByTagName, then loop through:

var allClearInputs = document.getElementsByTagName("input");
for(ac=0;ac<allClearInputs.length;ac=ac+1){
    if(allClearInputs[ac].id){
        var thisNameArr = allClearInputs[ac].id.split("-");
        var thisName = thisNameArr[0];
        if(thisName == checkName){
            if((actionType == 'all' && allClearInputs[ac].checked == false) || (actionType == 'clear' && allClearInputs[ac].checked == true)){
                allClearInputs[ac].click();
            }
        }
    }
}

Works perfectly with: for(ac=0;ac<allClearInputs.length;ac=ac+1){
Fails miserably with: for(var ac in allClearInputs)

8paw