forEach 方法 (Array) (JavaScript)
为数组中的每个元素执行指定操作。

array1.forEach(callbackfn[, thisArg])
|
参数 |
定义 |
|---|---|
|
array1 |
必选。一个数组对象。 |
|
callbackfn |
必选。最多可以接受三个参数的函数。对于数组中的每个元素,forEach 都会调用 callbackfn 函数一次。 |
|
thisArg |
可选。 callbackfn 函数中的 this 关键字可引用的对象。如果省略 thisArg,则 undefined 将用作 this 值。 |
如果 callbackfn 参数不是函数对象,则将引发 TypeError 异常。
Exception Condition
对于数组中出现的每个元素,forEach 方法都会调用 callbackfn 函数一次(采用升序索引顺序)。将不会为数组中缺少的元素调用回调函数。
除了数组对象之外,forEach 方法可由具有 length 属性且具有已按数字编制索引的属性名的任何对象使用。
回调函数的语法如下所示:
function callbackfn(value, index, array1)
你可使用最多三个参数来声明回调函数。
回调函数的参数如下所示。
|
回调参数 |
定义 |
|---|---|
|
Value |
数组元素的值。 |
|
index |
数组元素的数字索引。 |
|
array1 |
包含该元素的数组对象。 |
forEach 方法不直接修改原始数组,但回调函数可能会修改它。下表描述了在 forEach 方法启动后修改数组对象所获得的结果。
|
forEach 方法启动后的条件 |
元素是否传递给回调函数? |
|---|---|
|
在数组的原始长度之外添加元素。 |
否。 |
|
添加元素以填充数组中缺少的元素。 |
是,如果该索引尚未传递给回调函数。 |
|
元素已更改。 |
是,如果该元素尚未传递给回调函数。 |
|
从数组中删除元素。 |
否,除非该元素已传递给回调函数。 |
下面的示例阐释了 forEach 方法的用法。
// Define the callback function.
function ShowResults(value, index, ar) {
document.write("value: " + value);
document.write(" index: " + index);
document.write("<br />");
} // Create an array.
var letters = ['ab', 'cd', 'ef']; // Call the ShowResults callback function for each
// array element.
letters.forEach(ShowResults); // Output:
// value: ab index: 0
// value: cd index: 1
// value: ef index: 2
在下面的示例中,callbackfn 参数包含回调函数的代码。
// Create an array.
var numbers = [10, 11, 12]; // Call the addNumber callback function for each array element.
var sum = 0;
numbers.forEach(
function addNumber(value) { sum += value; }
); document.write(sum);
// Output: 33
下面的示例阐释了 thisArg 参数的用法,该参数指定可对其引用 this 关键字的对象。
// Define the object that contains the callback function.
var obj = {
showResults: function(value, index) {
// Call calcSquare by using the this value.
var squared = this.calcSquare(value); document.write("value: " + value);
document.write(" index: " + index);
document.write(" squared: " + squared);
document.write("<br />");
},
calcSquare: function(x) { return x * x }
}; // Define an array.
var numbers = [5, 6]; // Call the showResults callback function for each array element.
// The obj is the this value within the
// callback function.
numbers.forEach(obj.showResults, obj); // Embed the callback function in the forEach statement.
// The obj argument is the this value within the obj object.
// The output is the same as for the previous statement.
numbers.forEach(function(value, index) { this.showResults(value, index) }, obj); // Output:
// value: 5 index: 0 squared: 25
// value: 6 index: 1 squared: 36
// value: 5 index: 0 squared: 25
// value: 6 index: 1 squared: 36
要求
在以下文档模式中受支持:Internet Explorer 9 标准模式、Internet Explorer 10 标准模式和 Internet Explorer 11 标准模式。此外,也在应用商店应用(Windows 8 和 Windows Phone 8.1)中受支持。请参阅版本信息。
在以下文档模式中不受支持:Quirks、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式。
实例:
var data=[1,2,3,4,5,6];
var sum=0;
data.forEach(function(v){//其中的v就是数组的值 123456
sum+=v;})
document.write(sum+"<br>");//打印出来是21
data.forEach(function(o,p,q){//分别对应:数组元素,元素的索引,数组本身
q[p]=o+1;
})
document.write(data);
注意:forEach无法在所有元素都传递给调用的函数之前终止(而for循环却有break方法),如果要提前终止,必须把forEach放在try块中,并能抛出一个异常。如果forEach()调用的函数抛出foreach.break异常,循环会提前终止:
function foreach(a,b,c){
try{
a.forEach(b,c);
}catch(e){
if(e===foreach.break)return;
else throw e;
}
}
foreach.break=new Error("StopIteration");
}
forEach 方法 (Array) (JavaScript)的更多相关文章
- reduce 方法 (Array) (JavaScript)
对数组中的所有元素调用指定的回调函数.该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供. 语法 array1.reduce(callbackfn[, in ...
- lastIndexOf 方法 (Array) (JavaScript)
lastIndexOf 方法 (Array) (JavaScript) 返回指定的值在数组中的最后一个匹配项的索引. 语法 array1.lastIndexOf(searchEleme ...
- JavaScript forEach方法
最近看了一些html5和js方面的书,受益匪浅,因为看的东西比较多,却都没有怎么静心来做整理,慢慢来吧,可能最近自己有点儿小紧张.今天跟大家分享下JavaScript的forEach方法(其实是从&l ...
- javascript forEach方法与jQuery each区别
1.forEach方法 语法: array.forEach(function(currentValue, index, arr), thisValue) 参数: 示例: <!DOCTYPE ht ...
- JavaScript forEach() 方法
JavaScript forEach() 方法 JavaScript Array 对象 实例 列出数组的每个元素: <button onclick="numbers.forEach( ...
- JavaScript - reduce方法,reduceRight方法 (Array)
JavaScript - reduce方法 (Array) 解释:reduce() 方法接收一个函数作为累加器(accumulator),数组 中的每个值(从左到右)开始合并,最终为一个值. 语法:a ...
- 数组的方法之(Array.prototype.forEach() 方法)
forEach() 方法对数组的每个元素执行一次提供的函数. 注意: 没有返回一个新数组 并且 没有返回值! 应用场景:为一些相同的元素,绑定事件处理器! const arr = ['a', 'b', ...
- forEach方法如何跳出循环
1.for方法跳出循环 function getItemById(arr, id) { var item = null; for (var i = 0; i < arr.length; i++) ...
- js 判断数组包含某值的方法 和 javascript数组扩展indexOf()方法
var questionId = []; var anSwerIdValue = []; ////javascript数组扩展indexOf()方法 Array.prototype.indexOf ...
随机推荐
- PHPCMS V9教程之快速入门
这篇文章要为大家来介绍PHPCMS V9这个系统的一些基本知识,PHPCMS是基于面向对象的,严格的安装MVC开发模式开发的CMS系统,同时他还是一个非 常不错的PHP框架.下面我们一起看一下PHPC ...
- C#时间转整型(时间戳),模仿php strtotime函数的部分功能
今天需要将一个基于MS SQL数据库的新闻系统数据导入phpcms v9,源系统新闻日期格式为"2014-01-15 10:45:49",而phpcms中使用的是整型时间戳,在ph ...
- phalcon 前端代码结构
phalcon 前端举例: (1) baisic.phtml + basic_ajax_get.phtml + basic_ajax_post.phtml (2) basic_get.phtml ...
- Java计时器Timer和TimerTask用法
package com.sy.game.test; import java.util.Timer; import java.util.TimerTask; public class TimeTask ...
- Unity关于用LoadLevelAdditiveAsync导致新场景的Navmesh数据不正确Loading条的实践
为了解决用Application.LoadLevelAdditiveAsync 导致新场景的Navmesh数据不正确(我们用的是4.63),我们现在loading条做法是先切到Loading的场景,然 ...
- (转)C++0x语言新特性一览
转自:http://blog.csdn.net/zwvista/article/details/2429781 原文请见http://en.wikipedia.org/wiki/C%2B%2B0x. ...
- ios 中使用https的知识
先看文章,这篇文章说的是使用AFNetworing进行https时的事项,十分好!http://blog.cnbang.net/tech/2416/ ios中使用https,主要就是使用NSURLCr ...
- ffmpeg-20160718-git-bin.7z
官方 2016-07-18 发布的bin,彻底不支持 xp. ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] + ...
- ffmpeg-20160628-git-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- java入门第一季2
1. 变量:在java中,我们通过三个元素描述变量:变量类型,变量名以及变量值 注意:java中的标点符号都是英文的 2. 变量名= 首字母+其余部分 字母,+ 字母 下划线, 数字 $ ...