JavaScript Array hierarchy returns more values

It's that simple, I am puzzled. I have the following:

var x = 'shrimp';    
var stypes = new Array('shrimp', 'crabs', 'oysters', 'fin_fish', 'crawfish', 'alligator');
for (t in stypes) {
    if (stypes[t] != x) {
        alert(stypes[t]);
    }
}

      

After the values ​​have been iterated over, it starts to return a dozen functions like

function (iterator, context) {
    var index = 0;
    iterator = iterator.bind(context);
    try {
        this._each(function (value) {iterator(value, index++);});
    } catch (e) {
        if (e != $break) {
            throw e;
        }
    }
    return this;
}

      

What's happening?

Edit: In these scripts I am using http://script.aculo.us/prototype.js and http://script.aculo.us/scriptaculous.js Now I remember prototyping extending arrays and I am pretty sure that is part his. How do I deal with this?

+2


source to share


4 answers


The enumeration for

will loop through every element of the object you passed it over. In this case, an array that has both members and passed elements functions.

You can rewrite your for loop to check if there is typeof stypes[t] == "function"

or yada yada. But IMO you better just change your loop to elements only.

for(var i = 0, t; t = stypes[i]; ++i){
    if (t != x) {
        alert(t);
    }
}

      

Or

for(var i = 0; i < stypes.length; ++i){
    if (stypes[i] != x) {
        alert(stypes[i]);
    }
}

      



I want to move my last comment to the answer to add a warning notice for the first type of loop.

from Simon Willison "Reintroducing JavaScript" .

for (var i = 0, item; item = a[i]; i++) {
    // Do something with item
}

      

Here we are creating two variables. The assignment in the middle of the for loop is also checked for plausibility - if it succeeds, the loop continues. As I increment each time the elements from the array will be assigned to the element in a sequential order. The loop stops when "false" (eg undefined).

Note that this trick should only be used for arrays that you know do not contain "false" values ​​(arrays of objects or DOM nodes, for example). If you are repeating numeric data that can include 0 or string data that can include an empty string, you should use the i, j idiom.

+7


source


which you want to do:

for (var i in object) {
    if (!object.hasOwnProperty(i))
        continue;
    ... do stuff ...
}

      

As for ... in an enumeration, it iterates over all properties (enumerable or otherwise) that exist both on the object and on its prototype chain. Validation hasOwnProperty

restricts iteration to only these properties on the actual object you want to enumerate.



ES5 does things a little better for library developers (and helps avoid it), but we won't see that in a porting browser for quite some time now :-(

[edit: replacing return with a continuation. lalalalala;)]

+3


source


Since the prototype has expanded the array for your convenience, you should use it. Your example can be rewritten as:

var x = 'shrimp';    
var stypes = new Array('shrimp', 'crabs', 'oysters', 'fin_fish', 'crawfish', 'alligator');
stypes.without(x).each(alert);

      

+1


source


It should be

for (t in stypes) {
    if (t != x) {
        alert(t);
    }
}

      

-3


source







All Articles