TypedArray.from() 方法 从一个类数组或者可迭代对象中创建一个新类型数组。 这个方法和 Array.from()类似。

语法

TypedArray.from(source[, mapFn[, thisArg]])

where TypedArray is one of:

Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array

参数

source
想要转换为类型数组的类数组或者可迭代对象。
mapFn
可选参数。如果指定了该参数,则最后生成的类型数组会经过该函数的加工处理后再返回。
thisArg
可选参数。执行 mapFn 函数时 this 的值。

返回值

一个新的 TypedArray 实例。

 描述

TypedArray.from() 允许你从下面两者来创建数组:

TypedArray.from() 方法有一个可选参数 mapFn, 让你可以在最后生成的类型数组上再执行一次 map 方法后再返回。也就是说 TypedArray.from(obj, mapFn, thisArg) 和TypedArray.from(Array.prototype.map.call(obj, mapFn, thisArg)) 是等价的。

 from()length 属性为1.

Array.from() 和 TypedArray.from()之间有一些微妙的区别:

示例

// Set (iterable object)
var s = new Set([1, 2, 3]);
Uint8Array.from(s);
// Uint8Array [ 1, 2, 3 ]


// String
Int16Array.from('123');                      
// Int16Array [ 1, 2, 3 ]


// Using an arrow function as the map function to
// manipulate the elements
Float32Array.from([1, 2, 3], x => x + x);      
// Float32Array [ 2, 4, 6 ]


// Generate a sequence of numbers
Uint8Array.from({length: 5}, (v, k) => k);    
// Uint8Array [ 0, 1, 2, 3, 4 ]

Polyfill 

You can partially work around this by inserting the following code at the beginning of your scripts, allowing use of much of the functionality of from() in implementations that do not natively support it.

if (!Int8Array.__proto__.from) {
    (function () {
        Int8Array.__proto__.from = function (obj, func, thisObj) {

            var typedArrayClass = Int8Array.__proto__;
            if(typeof this !== 'function') {
                throw new TypeError('# is not a constructor');
            }
            if (this.__proto__ !== typedArrayClass) {
                throw new TypeError('this is not a typed array.');
            }
 
            func = func || function (elem) {
                    return elem;
                };

            if (typeof func !== 'function') {
                throw new TypeError('specified argument is not a function');
            }

            obj = Object(obj);
            if (!obj['length']) {
                return new this(0);
            }
            var copy_data = [];
            for(var i = 0; i < obj.length; i++) {
                copy_data.push(obj[i]);
            }

            copy_data = copy_data.map(func, thisObj);

            var typed_array = new this(copy_data.length);
            for(var i = 0; i < typed_array.length; i++) {
                typed_array[i] = copy_data[i];
            }
            return typed_array;
        }
    })();
}

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
%TypedArray%.from
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
%TypedArray%.from
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
fromChrome Full support 45Edge Full support 14Firefox Full support 38IE No support NoOpera No support NoSafari Full support 10WebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android Full support 38Opera Android No support NoSafari iOS Full support 10Samsung Internet Android No support Nonodejs Full support 4.0.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also