The subarray()
method returns a new TypedArray on the same ArrayBuffer
store and with the same element types as for this TypedArray object. The begin
offset is inclusive and the end
offset is exclusive. TypedArray is one of the typed array types.
typedarray.subarray([begin [,end]])
begin
to the end of the array are included in the new view.A new TypedArray
object.
The range specified by begin
and end
is clamped to the valid index range for the current array; if the computed length of the new array would be negative, it's clamped to zero. If either begin
or end
is negative, it refers to an index from the end of the array instead of from the beginning.
Also note that this is creating a new view on the existing buffer; changes to the new object's contents will impact the original object and vice versa.
subarray
methodvar buffer = new ArrayBuffer(8); var uint8 = new Uint8Array(buffer); uint8.set([1,2,3]); console.log(uint8); // Uint8Array [ 1, 2, 3, 0, 0, 0, 0, 0 ] var sub = uint8.subarray(0,4); console.log(sub); // Uint8Array [ 1, 2, 3, 0 ]
Specification | Status | Comment |
---|---|---|
Typed Array Specification | Obsolete | Superseded by ECMAScript 6. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'TypedArray.prototype.subarray' in that specification. | Standard | Initial definition in an ECMA standard. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'TypedArray.prototype.subarray' in that specification. | Draft |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 7 | 12 | 4.0 (2) | No support | 11.6 | 5.1 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 4.0 | (Yes) | 4.0 (2) | 10 | 11.6 | 4.2 |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray