我如何通过一个序列中的所有输入,使用JavaScript?
当前回答
这是一个非分散列表的 iterator,指数从0开始,这是处理document.getElementsByTagName或document.querySelectorAll时的典型场景)
function each( fn, data ) {
if(typeof fn == 'string')
eval('fn = function(data, i){' + fn + '}');
for(var i=0, L=this.length; i < L; i++)
fn.call( this[i], data, i );
return this;
}
Array.prototype.each = each;
使用例子:
例子 #1
var arr = [];
[1, 2, 3].each( function(a){ a.push( this * this}, arr);
arr = [1, 4, 9]
例子 #2
each.call(document.getElementsByTagName('p'), "this.className = data;",'blue');
每個 p 標籤都會得到 class="blue"
例子 #3
each.call(document.getElementsByTagName('p'),
"if( i % 2 == 0) this.className = data;",
'red'
);
每個其他 p 標籤都會得到 class="red">
例子 #4
each.call(document.querySelectorAll('p.blue'),
function(newClass, i) {
if( i < 20 )
this.className = newClass;
}, 'green'
);
最后,第一个20个蓝色P标签变成绿色
使用字符串作为函数时要小心:函数是由背景外创建的,并且应该仅在您对变量调节的确定情况下使用。
其他回答
最接近您的想法的方式将是使用 Array.forEach() 接受关闭函数,该函数将适用于序列的每个元素。
myArray.forEach(
(item) => {
// Do something
console.log(item);
}
);
另一种可行的方式是使用 Array.map() 以相同的方式工作,但它也需要所有值,你返回并返回它们在一个新的序列(基本上将每个元素地图到一个新的),如下:
var myArray = [1, 2, 3];
myArray = myArray.map(
(item) => {
return item + 1;
}
);
console.log(myArray); // [2, 3, 4]
你可以使用:
ForEach theArray.forEach(功能(序列,索引) { console.log(索引); console.log(序列); }; for(var i=0; i<theArray.length; i++) { console.log(i) } 地图 theArray.map(x => console.log(x));地图 theArray.filter(x => console.log(x));
而且还有很多其他人为 iteration。
Lambda Syntax 通常不适用于 Internet Explorer 10 或更低版本。
我通常使用
[].forEach.call(arrayName,function(value,index){
console.log("value of the looped element" + value);
console.log("index of the looped element" + index);
});
如果您是一个 jQuery 粉丝,并且已经有一个 jQuery 文件运行,您应该逆转指数和值参数的位置。
$("#ul>li").each(function(**index, value**){
console.log("value of the looped element" + value);
console.log("index of the looped element" + index);
});
表演
细节
在测试中,我们计算序列元素的总数,我对小序列(10个元素)和大序列(1M元素)进行测试,并将它们分为三个组:
A - 测试B - 测试C - 其他/替代方法
Cross 浏览器结果
所有测试浏览器的结果
浏览器*
配有10个元素
此分類上一篇
配备1000元元素
Chrome 的结果:您可以在这里在您的机器上进行测试
此分類上一篇
我知道这是一个古老的帖子,而且已经有很多很好的答案。 为了更完整的我想象我会扔在另一个使用AngularJS. 当然,这只适用于如果你使用Angular,显然,然而,我会喜欢把它无论如何。
第一個論點是對象(命令)以 iterate 上,第二個論點是 iterator 函數,而選擇的第三個論點是對象背景(基本上提到在輪內的「這」)。
var temp = [1, 2, 3];
angular.forEach(temp, function(item) {
//item will be each element in the array
//do something
});
var temp = [1, 2, 3];
var temp2 = [];
angular.forEach(temp, function(item) {
this.push(item); //"this" refers to the array passed into the optional third parameter so, in this case, temp2.
}, temp2);
angular.forEach(temp, function(item) {
temp2.push(item);
});
福利
易于阅读 易于写作 如果可用, angular.forEach 将使用 ES5 forEach loop. 现在,我会得到效率,因为 forEach loop 比 loops 更慢。
angular.forEach(obj1.results, function(result1) {
angular.forEach(obj2.results, function(result2) {
if (result1.Value === result2.Value) {
//do something
}
});
});
//exact same with a for loop
for (var i = 0; i < obj1.results.length; i++) {
for (var j = 0; j < obj2.results.length; j++) {
if (obj1.results[i].Value === obj2.results[j].Value) {
//do something
}
}
}
推荐文章
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- 如何在c#中连接列表?
- 应该……接住环内还是环外?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?