首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

typedArray.forEach

forEach()方法对类型化数组的每个元素调用提供的函数。 这个方法的算法和Array.prototype.forEach()相同。TypedArray是这里的类型化数组类型之一。

语法

代码语言:javascript
复制
typedarray.forEach(callback[, thisArg])

参数

callback产生新的类型化数组的元素的函数,接受三个函数:currentValue类型化数组中要处理的当前元素index类型化数组中要处理的当前元素的下标arrayforEach()在其上调用的类型化数组thisArg可选,执行callback时作为this的值。

返回值

undefined.

描述

forEach方法对类型化数组中的元素按升序调用提供的 callback函数。 它不会对删除或者省略的下标调用,但是会对存在并且值为undefined的元素调用。

callback三个参数调用:

  • 元素的值

  • 元素下标

  • 被遍历的类型化数组

如果将thisArg参数提供给forEach,它会在调用时传递给callback,作为它的this值。否则,会传递undefined作为它的this值。 callback最终观测到的this值由用于决定函数可见的this值的一般规则来决定。

forEach处理的元素范围在callback调用之前就确定了。 在forEach调用之后添加到数组的元素不会由callback访问。 如果类型化数组的现有元素被改变,或被删除,它们传给callback的值是forEach访问它们时候的值。已删除的元素不会被访问。

forEach()对每个数组元素执行一次callback函数;不像every()some(),它始终返回undefined

示例

记录类型化数组的内容

下面的代码为数组中的每个元素记录一行日志:

代码语言:javascript
复制
function logArrayElements(element, index, array) {
  console.log('a[' + index + '] = ' + element);
}

new Uint8Array([0, 1, 2, 3]).forEach(logArrayElements);
// logs:
// a[0] = 0
// a[1] = 1
// a[2] = 2
// a[3] = 3

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of '%TypedArray%.prototype.forEach' in that specification.

Standard

Initial definition.

ECMAScript 2017 Draft (ECMA-262)The definition of '%TypedArray%.prototype.forEach' in that specification.

Draft

浏览器兼容性

Feature

Chrome

Firefox (Gecko)

Edge

Internet Explorer

Opera

Safari

Basic support

(Yes)

38 (38)

(Yes)

No support

?

10

Feature

Android

Chrome for Android

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

?

?

38.0 (38)

?

?

?

扫码关注腾讯云开发者

领取腾讯云代金券