如何在jQuery中循环遍历数组? [英] How to loop through array in jQuery?

查看:276
本文介绍了如何在jQuery中循环遍历数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历一个数组。我有以下代码:

I am trying to loop through an array. I have the following code:

 var substr = currnt_image_list.split(','); //This will split up 21,32,234,223,

我试图从阵列中获取所有数据。有人可以带我走正确的道路吗?

Am trying to get all the data out of the array. Can some one lead me in the right path please?

推荐答案




(更新:我的其他答案更彻底地列出了非jQuery选项。下面的第三个选项, jQuery.each ,但不在其中。)


(Update: My other answer here lays out the non-jQuery options much more thoroughly. The third option below, jQuery.each, isn't in it though.)

三个选项:

var i;
for (i = 0; i < substr.length; ++i) {
    // do something with `substr[i]`
}

优点:直接前进,不依赖于jQuery,易于理解,没有保留含义的问题循环体内的这个,没有不必要的函数调用开销(例如,在理论中更快,尽管事实上你必须有很多元素,你可能遇到其他问题; 详情)。

Advantages: Straight-forward, no dependency on jQuery, easy to understand, no issues with preserving the meaning of this within the body of the loop, no unnecessary overhead of function calls (e.g., in theory faster, though in fact you'd have to have so many elements that the odds are you'd have other problems; details).

从ECMAScript5开始,数组上有一个 forEach 函数,这样可以很容易地遍历数组:

As of ECMAScript5, arrays have a forEach function on them which makes it easy to loop through the array:

substr.forEach(function(item) {
    // do something with `item`
});

链接到文档

(注意:还有很多其他功能,不只是 forEach ;有关详情,请参阅上述答案。)

(Note: There are lots of other functions, not just forEach; see the answer referenced above for details.)

优点:声明式,如果你有一个方便的话,可以使用迭代器的预建函数,如果你的循环体是复杂的范围函数调用有时是有用的,在您的包含范围内不需要 i 变量。

Advantages: Declarative, can use a prebuilt function for the iterator if you have one handy, if your loop body is complex the scoping of a function call is sometimes useful, no need for an i variable in your containing scope.

缺点:并非所有浏览器都拥有它,尽管大多数情况都是如此,并且您始终可以使用ES5填充程序(搜索将列出几个)在尚未拥有它的浏览器上提供它。如果你在包含的代码中使用 this ,你必须将它粘贴在一个变量中,这样你就可以在函数中使用它或者将它作为第二个参数传递给 forEach ,因为在迭代函数中,将是 undefined (在严格模式下)或非严格模式下的全局对象(窗口),除非你给 forEach 一个特定的值它。

Disadvantages: Not all browsers have it yet, although most do, and you can always use an ES5 shim (a search will list several) to provide it on browsers that don't have it yet. If you're using this in the containing code, you have to stick it in a variable so you can use it within the function or pass it as a second argument to forEach, since within the iteration function, this will be undefined (in strict mode) or the global object (window) in non-strict mode unless you give forEach a specific value for it.

jQuery.each(substr, function(index, item) {
    // do something with `item` (or `this` is also `item` if you like)
});

文档链接

优势:与相同的所有优点forEach ,加上你知道它就在那里,因为你正在使用jQuery。

Advantages: All of the same advantages as forEach, plus you know it's there since you're using jQuery.

缺点:如果你正在使用这个在包含的代码中,你必须将它粘贴在一个变量中,这样你才能在函数中使用它,因为这个表示函数中的其他内容。

Disadvantages: If you're using this in the containing code, you have to stick it in a variable so you can use it within the function, since this means something else within the function.

你可以通过使用这个的事情=http://api.jquery.com/jQuery.proxy =noreferrer> $。proxy

You can avoid the this thing though, by either using $.proxy:

jQuery.each(substr, $.proxy(function(index, item) {
    // do something with `item` (`this` is the same as it was outside)
}, this));

...或函数#bind

jQuery.each(substr, function(index, item) {
    // do something with `item` (`this` is the same as it was outside)
}.bind(this));

...或在ES2015(ES6)中,箭头功能:

...or in ES2015 ("ES6"), an arrow function:

jQuery.each(substr, (index, item) => {
    // do something with `item` (`this` is the same as it was outside)
});



要做什么:



使用 for..in 为此(或者如果你这样做,请使用适当的安全措施)。你会看到有人说(事实上,这里有一个简短的回答说),但 for..in 并没有做到很多人认为它做的事情(它做一些更有用的东西!)。具体来说, for..in 循环访问对象的可枚举属性名称(而不是数组的索引)。由于数组是对象,并且它们唯一可枚举的属性默认情况下是索引,因此它似乎主要是在一个平淡的部署中工作。但是,你可以直接使用它并不是一个安全的假设。这是一个探索: http://jsbin.com/exohi/3

What NOT to do:

Don't use for..in for this (or if you do, do it with proper safeguards). You'll see people saying to (in fact, briefly there was an answer here saying that), but for..in does not do what many people think it does (it does something even more useful!). Specifically, for..in loops through the enumerable property names of an object (not the indexes of an array). Since arrays are objects, and their only enumerable properties by default are the indexes, it mostly seems to sort of work in a bland deployment. But it's not a safe assumption that you can just use it for that. Here's an exploration: http://jsbin.com/exohi/3

我应该软化上面的不要。如果您正在处理稀疏数组(例如,该数组总共有15个元素,但由于某种原因,它们的索引在0到150,000范围内散布,所以长度如果您使用适当的保护措施,例如 hasOwnProperty ,并检查属性名称是否为数字(请参阅上面的链接),,<$> c> for..in 可以避免大量不必要的循环,因为只会枚举填充的索引。

I should soften the "don't" above. If you're dealing with sparse arrays (e.g., the array has 15 elements in total but their indexes are strewn across the range 0 to 150,000 for some reason, and so the length is 150,001), and if you use appropriate safeguards like hasOwnProperty and checking the property name is really numeric (see link above), for..in can be a perfectly reasonable way to avoid lots of unnecessary loops, since only the populated indexes will be enumerated.

这篇关于如何在jQuery中循环遍历数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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