Why isit when I do
var footnoteLinks = [1,2,3];
for (var i in footnoteLinks) {
document.write(footnoteLinks[i] + ", ");
}
What I get is something like …
1, 2, 3, function () { return lower; }, function Array() { [native code] }, function pop() { [native code] }, function push() { [native code] }, function reverse() { [native code] }, function shift() { [native code] }, function sort() { [native code] }, function splice() { [native code] }, function unshift()
Why is that? Whats with the functions etc… I think it worked ok b4, I dunno what caused this now, it seems the same for all browsers I tried. Firefox 3.6, Chrome 6 (i think?), IE9
The
for(var in obj)is for iterating over the properties of an object. You’re getting the properties of the Array object you’ve created.You want a more traditional looping/index construct:
Some JavaScript runtimes also have
mapandreducemethods on Array objects, but this isn’t guaranteed. Most JavaScript libraries have something like this (or perhaps aneachmethod), though.