我试图通过一个数组循环。我有以下代码:
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
我试图把所有的数据从数组。有谁能指引我正确的道路吗?
当前回答
通过数组/字符串迭代的替代方法有副作用
Var STR = '21,32,234,223'; Var substr = str.split(','); Substr.reduce ((a,x)=> console.log('reduce',x), 0) //返回undefined 字符串的子串。Every (x=> {console.log(' Every ',x);返回true}) //返回true 字符串的子串。Some (x=> {console.log(' Some ',x);}) //返回false 字符串的子串。地图(x = > console.log(“地图”,x));//返回数组 Str.replace (/(\d+)/g, x=> console.log('replace',x)) //返回字符串
其他回答
使用jQuery each()。还有其他方法,但每一种都是为这个目的而设计的。
$.each(substr, function(index, value) {
alert(value);
});
不要在最后一个数字后面加逗号。
通过数组/字符串迭代的替代方法有副作用
Var STR = '21,32,234,223'; Var substr = str.split(','); Substr.reduce ((a,x)=> console.log('reduce',x), 0) //返回undefined 字符串的子串。Every (x=> {console.log(' Every ',x);返回true}) //返回true 字符串的子串。Some (x=> {console.log(' Some ',x);}) //返回false 字符串的子串。地图(x = > console.log(“地图”,x));//返回数组 Str.replace (/(\d+)/g, x=> console.log('replace',x)) //返回字符串
(更新:我的另一个答案在这里更彻底地列出了非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可能是一种非常合理的方式,可以避免大量不必要的循环,因为只有填充的索引将被枚举。
for(var key in substr)
{
// do something with substr[key];
}
美元。地图数据,功能(elem) {...})
$.map(data,function(elem){
console.log(elem);
})