我试图通过一个数组循环。我有以下代码:
var currnt_image_list= '21,32,234,223';
var substr = currnt_image_list.split(','); // array here
我试图把所有的数据从数组。有谁能指引我正确的道路吗?
我试图通过一个数组循环。我有以下代码:
var currnt_image_list= '21,32,234,223';
var substr = currnt_image_list.split(','); // array here
我试图把所有的数据从数组。有谁能指引我正确的道路吗?
当前回答
jQuery.each ()
jQuery.each ()
jQuery.each(array, callback)
数组的迭代
jQuery.each(array, function(Integer index, Object value){});
对象的迭代
jQuery.each(object, function(string propertyName, object propertyValue){});
例子:
var substr = [1, 2, 3, 4]; $.each(substr , function(index, val) { 控制台.log(索引,值) }); var myObj = { firstName: “skyfoot”}; $.each(myObj, function(propName, propVal) { console.log(propName, propVal); }); <script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script>
Javascript循环数组
for循环
for (initialExpression; condition; incrementExpression)
statement
例子
Var substr = [1,2,3,4]; //从0索引循环到最大索引 For (var I = 0;I < substr.length;我+ +){ console.log(“循环”,substr(我)) } / /反向循环 For (var I = substr.length-1;I >= 0;我(){ console.log(“反向”,substr(我)) } / /步骤循环 For (var I = 0;I < substr.length;我+ = 2){ console.log(“一步”,substr(我)) }
因为在
//dont really wnt to use this on arrays, use it on objects
for(var i in substr) {
console.log(substr[i]) //note i returns index
}
对的
for(var i of subs) {
//can use break;
console.log(i); //note i returns value
}
forEach
substr.forEach(function(v, i, a){
//cannot use break;
console.log(v, i, a);
})
资源
MDN循环和迭代器
其他回答
使用jQuery的each()函数。
这里有一个例子:
$.each(currnt_image_list.split(','), function(index, value) {
alert(index + ': ' + value);
});
使用jQuery each()。还有其他方法,但每一种都是为这个目的而设计的。
$.each(substr, function(index, value) {
alert(value);
});
不要在最后一个数字后面加逗号。
你可以使用for()循环:
var things = currnt_image_list.split(',');
for(var i = 0; i < things.length; i++) {
//Do things with things[i]
}
试试这个:
$.grep(array, function(element) {
})
(更新:我的另一个答案在这里更彻底地列出了非jquery选项。下面的第三个选项是jQuery。不过,它们都不在里面。)
四个选项:
通用的循环:
var i;
for (i = 0; i < substr.length; ++i) {
// do something with `substr[i]`
}
或在ES2015+:
for (let i = 0; i < substr.length; ++i) {
// do something with `substr[i]`
}
优点:直接,不依赖jQuery,易于理解,在循环体中保留其含义没有问题,没有不必要的函数调用开销(例如,理论上更快,尽管实际上你必须有这么多元素,可能会有其他问题;详细信息)。
ES5«forEach操作:
从ECMAScript5开始,数组上有一个forEach函数,这使得循环数组变得很容易:
substr.forEach(function(item) {
// do something with `item`
});
链接到文档
(注意:还有很多其他函数,不仅仅是forEach;详见上面的答案。)
优点:声明性,可以使用预先构建的迭代器函数,如果你有一个方便的,如果你的循环体是复杂的,函数调用的作用域有时是有用的,不需要在你的包含范围i变量。
Disadvantages: If you're using this in the containing code and you want to use this within your forEach callback, you have to either A) Stick it in a variable so you can use it within the function, B) Pass it as a second argument to forEach so forEach sets it as this during the callback, or C) Use an ES2015+ arrow function, which closes over this. If you don't do one of those things, in the callback this will be undefined (in strict mode) or the global object (window) in loose mode. There used to be a second disadvantage that forEach wasn't universally supported, but here in 2018, the only browser you're going to run into that doesn't have forEach is IE8 (and it can't be properly polyfilled there, either).
ES2015 +的的:
for (const s of substr) { // Or `let` if you want to modify it in the loop body
// do something with `s`
}
请参阅这个答案顶部的答案链接,了解如何工作的详细信息。
优点:简单、直接,为数组中的条目提供了一个包含作用域的变量(或常量,如上面所示)。
缺点:在任何IE版本中都不支持。
jQuery.each:
jQuery.each(substr, function(index, item) {
// do something with `item` (or `this` is also `item` if you like)
});
(链接到文档)
优点:所有与forEach相同的优点,加上你知道它的存在,因为你使用的是jQuery。
缺点:如果你在包含它的代码中使用它,你必须把它放在一个变量中,这样你就可以在函数中使用它,因为这意味着在函数中有其他的东西。
你可以通过使用$.proxy来避免这个事情:
jQuery.each(substr, $.proxy(function(index, item) {
// do something with `item` (`this` is the same as it was outside)
}, this));
...或函数绑定:
jQuery.each(substr, function(index, item) {
// do something with `item` (`this` is the same as it was outside)
}.bind(this));
...或者在ES2015(“ES6”)中,一个箭头函数:
jQuery.each(substr, (index, item) => {
// do something with `item` (`this` is the same as it was outside)
});
不要做什么:
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的范围内,因此长度为150,001),并且如果您使用适当的保护措施,如hasOwnProperty,并检查属性名是否真的是数字(参见上面的链接),for..in可能是一种非常合理的方式,可以避免大量不必要的循环,因为只有填充的索引将被枚举。