数组迭代方法对每个数组项进行操作。
Array.forEach()
forEach() 方法为每个数组元素调用一次函数(回调函数)。
实例
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);
function myFunction(value, index, array) {
txt = txt + value + "<br>";
}
注释:该函数接受 3 个参数:
项目值
项目索引
数组本身
上面的例子只用了 value 参数。这个例子可以重新写为:
实例
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);
function myFunction(value) {
txt = txt + value + "<br>";
}
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64583.shtml