JavaScript的数组迭代比返回值的更多 [英] JavaScript Array Iteration returning more than values

查看:129
本文介绍了JavaScript的数组迭代比返回值的更多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如此简单,我百思不得其解。我有以下几点:

  VAR X ='虾';
变种stypes =新阵列(虾','蟹','牡蛎','fin_fish','小龙虾','鳄鱼');
对(在stypes T){
    如果(stypes [T]!= X){
        警报(stypes [T]);
    }
}

一旦值已遍历它开始返回十几功能,如

 功能(迭代器,上下文){
    变种索引= 0;
    迭代= iterator.bind(上下文);
    尝试{
        this._each(函数(值){迭代器(价值指数++);});
    }赶上(E){
        如果(E!= $休息){
            Ë扔掉;
        }
    }
    返回此;
}

到底是什么回事?

编辑:在这些脚本,我使用 http://script.aculo.us/prototype.js http://script.aculo.us/scriptaculous.js 我记得现在读关于路延伸原型阵列和我打赌,这是它的一部分。我该如何处理呢?


解决方案

枚举是要投奔的每个的对象的成员你通过它。在这种情况下一个数组,其中恰好有功能的成员以及传递的元素。

您可以重新编写循环,以检查是否的typeof stypes [T] ==功能或内容十分重要。但国际海事组织你最好只是修改你的循环只能元素..

 为(VAR I = 0,T; T = stypes [I]; ++ I){
    如果(T!= X){
        警报(T);
    }
}

或者

 为(VAR I = 0; I< stypes.length ++我){
    如果(stypes由[i]!= X){
        警报(stypes [I]);
    }
}

我要迁移我最后的评论最多的答案添加一个警告通知的第一类型的循环。

威利森的重新介绍给JavaScript ..

 为(VAR I = 0,项;项目= a [i];我++){
    //用做什么项目
}


  

在这里我们设置两个变量。
  在中间部分的分配
  for循环还测试
  真实性 - 如果它成功,
  继续循环。因为i递增
  每一次,从数组项将
  被分配给项在顺序
  订购。循环停止,当falsy
  项目被发现(如未定义)。


  
  

请注意,此招只应
  用于你知道不阵列
  含有falsy值(数组
  对象或DOM例如节点)。如果
  你迭代数值数据
  这可能包括一个0或字符串数​​据
  这可能包括空字符串
  你应该使用I,J语言,而不是


This is so simple I am baffled. 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]);
    }
}

Once the values have iterated it starts returning 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 the heck is going on?

Edit: In these scripts I am using http://script.aculo.us/prototype.js and http://script.aculo.us/scriptaculous.js I remember now reading about the way prototype extends arrays and I am betting this is part of it. How do I deal with it?

解决方案

The for enumeration is going to go over every member of the object you passed it. In this case an array, which happens to have functions as members as well as the elements passed.

You could re-write your for loop to check if typeof stypes[t] == "function" or yada yada. But IMO you are better off just modifying your looping to only elements..

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 wanted to migrate my last comment up to the answer to add the notice of the a caveat for the first type of loop.

from Simon Willison's "A re-introduction to JavaScript"..

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

Here we are setting up two variables. The assignment in the middle part of the for loop is also tested for truthfulness - if it succeeds, the loop continues. Since i is incremented each time, items from the array will be assigned to item in sequential order. The loop stops when a "falsy" item is found (such as undefined).

Note that this trick should only be used for arrays which you know do not contain "falsy" values (arrays of objects or DOM nodes for example). If you are iterating over numeric data that might include a 0 or string data that might include the empty string you should use the i, j idiom instead.

这篇关于JavaScript的数组迭代比返回值的更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆