typedArray.map
map()方法创建一个新的类型数组,其结果是在此类型数组中的每个元素上调用提供的函数。这个方法和Array.prototype.map()。 TypedArray是这里的类型数组类型之一。
语法
typedarray.map(callback[, thisArg])参数
callback生成新类型数组的元素的函数,取三个参数:currentValue正在处理的类型数组中的当前元素。index在类型数组中处理当前元素的索引。被调用的array数组map被调用。thisArg可选的。this执行时使用的值callback。
返回值
一个新的类型数组。
描述
该map方法callback按顺序为类型数组中的每个元素调用一次提供的函数,并从结果中构造一个新的类型数组。callback只对已经赋值的类型数组的索引被调用; 对于未定义的索引,已经被删除的或者从未被赋值的索引,不会调用它。
callback 被调用三个参数:元素的值,元素的索引和被遍历的类型化的数组对象。
如果thisArg提供了一个参数map,它将callback在被调用时传递给它,作为它的this值。否则,该值undefined将被传递以用作其this值。this最终可观察到的值callback是根据用于确定this函数所看到的通常规则来确定的。
map不会改变它被调用的类型数组(尽管callback如果调用,可能会这样做)。
处理的元素范围map在第一次调用之前设置callback。在map开始调用之后追加到数组的元素将不会被访问callback。如果类型数组的现有元素被更改或删除,则传递给它们的值callback将是map访问它们时的值; 被删除的元素不被访问。
示例
将一个类型数组映射到一个平方根的类型数组
下面的代码接受一个类型数组,并创建一个新的类型数组,其中包含第一个数组中的数字的平方根。
var numbers = new Uint8Array([1, 4, 9]);
var roots = numbers.map(Math.sqrt);
// roots is now: Uint8Array [1, 2, 3],
// numbers is still Uint8Array [1, 4, 9]使用包含参数的函数映射一个数字类型数组
下面的代码显示了当一个需要一个参数的函数与它一起使用时map的工作方式。该参数将自动分配给类型化数组的每个元素,作为通过原始类型化数组的映射循环。
var numbers = new Uint8Array([1, 4, 9]);
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now Uint8Array [2, 8, 18]
// numbers is still Uint8Array [1, 4, 9]规范
Specification | Status | Comment |
|---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray.prototype.map' in that specification. | Standard | Initial definition. |
ECMAScript 2017 Draft (ECMA-262)The definition of 'TypedArray.prototype.map' in that specification. | Draft | |
浏览器兼容性
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
Basic support | (Yes) | 38 (38) | No support | No support | No support |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
Basic support | No support | No support | 38.0 (38) | No support | No support | No support |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

