在 ECMAScript 2017(ES8)中,JavaScript 引入了新的 TypedArray 类型 Int8Array,该类可用于存储 8 位有符号整数。
本篇文章将详细介绍 Int8Array 类及其方法,并提供示例代码以帮助读者更好地理解。
Int8Array 类
Int8Array 类是 TypedArray 类型之一,它继承了同样是 TypedArray 类型的 ArrayBuffer 类。
使用 Int8Array 类时,我们需要先创建一个 ArrayBuffer,再将其传递给 Int8Array 的构造函数,这样就可以在 ArrayBuffer 上创建一个 Int8Array 数组:
// 创建一个包含 8 个元素的 Int8Array 数组 const buffer = new ArrayBuffer(8); const int8Array = new Int8Array(buffer);
由于 Int8Array 存储的是有符号整数,因此每个元素的值的范围为 -128 到 127。
创建 Int8Array 数组的方式
除了前面提到的以 ArrayBuffer 作为参数创建的方式外,还有以下两种方式可以创建 Int8Array 数组:
- 直接创建指定长度的 Int8Array 数组:
const int8Array = new Int8Array(8);
这种方式等同于以包含相同长度的 ArrayBuffer 作为参数创建 Int8Array 数组。
- 通过数组或可迭代对象创建 Int8Array 数组:
-- -------------------- ---- ------- ----- ----- - --- -- -- -- -- -- -- --- ----- --------- - --- ----------------- -- --------- --------- -- ----- -------- - - -------------------- - ----- -- ----- -- ----- -- ----- -- ----- -- ----- -- ----- -- ----- -- - -- ----- --------- - --- --------------------
在通过数组或可迭代对象创建 Int8Array 数组时,如果某个元素的值超出 -128 到 127 的范围,则会被截断为符合该范围的值。
获取 Int8Array 数组中的元素
Int8Array 数组的元素可以通过索引访问,与普通数组相同:
// 获取第一个元素 const firstElement = int8Array[0]; // 获取最后一个元素 const lastElement = int8Array[int8Array.length - 1];
设置 Int8Array 数组中的元素
我们可以通过索引设置 Int8Array 数组中的元素:
// 设置第一个元素的值为 10 int8Array[0] = 10; // 设置最后一个元素的值为 -20 int8Array[int8Array.length - 1] = -20;
Int8Array 方法
除了普通数组所支持的方法外,Int8Array 类还提供了以下方法:
1. Int8Array.BYTES_PER_ELEMENT
该属性表示 Int8Array 数组中每个元素的字节大小,其值为 1。
console.log(Int8Array.BYTES_PER_ELEMENT); // 1
2. Int8Array.from()
该静态方法可用于创建一个新的 Int8Array 数组,该数组中的元素与传入的参数相关。
-- -------------------- ---- ------- -- ------ --------- -- ----- ----- - --- -- -- -- -- -- -- --- ----- --------- - ---------------------- -- --------- --------- -- ----- -------- - - -------------------- - ----- -- ----- -- ----- -- ----- -- ----- -- ----- -- ----- -- ----- -- - -- ----- --------- - ------------------------- -- -- --- ---------- ----- ----- - --- -- -- -- -- -- -- --- ----- --------- - --------------------- - -- - - ---
在使用 map 函数处理数组中的元素时,需要注意传入 map 函数的参数应该是一个数字类型,否则会产生 NaN 或 Infinity。
3. Int8Array.of()
该静态方法可用于创建一个新的 Int8Array 数组,该数组中的元素与传入的参数一一对应。
const int8Array = Int8Array.of(1, 2, 3, 4, 5, 6, 7, 8);
4. Int8Array.prototype.copyWithin()
该方法可用于将数组中的一部分元素拷贝到另一部分元素位置上。
const int8Array = new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]); // 将数组中下标为 0 的元素拷贝到下标为 4 的位置上 int8Array.copyWithin(4, 0, 1);
5. Int8Array.prototype.every()
该方法可用于判断数组中的所有元素是否满足指定条件。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 判断所有元素是否大于 0 const allGreaterThanZero = int8Array.every(x => x > 0);
6. Int8Array.prototype.fill()
该方法可用于将数组中的全部或部分元素替换为指定值。
const int8Array = new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]); // 将数组的所有元素设置为 0 int8Array.fill(0); // 将数组中下标为 2~5 的元素设置为 -1 int8Array.fill(-1, 2, 5);
7. Int8Array.prototype.filter()
该方法可用于筛选数组中符合条件的元素。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 筛选所有大于 0 的元素 const positiveNumbers = int8Array.filter(x => x > 0);
8. Int8Array.prototype.find()
该方法可用于查找数组中第一个符合条件的元素,并返回该元素。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 查找第一个大于 0 的元素 const firstPositiveNumber = int8Array.find(x => x > 0);
9. Int8Array.prototype.findIndex()
该方法可用于查找数组中第一个符合条件的元素的下标,并返回该下标。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 查找第一个大于 0 的元素的下标 const index = int8Array.findIndex(x => x > 0);
10. Int8Array.prototype.forEach()
该方法可用于遍历数组中的每个元素,并对其进行操作。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 对数组中的每个元素加上 1 int8Array.forEach((value, index, array) => { array[index] = value + 1; });
11. Int8Array.prototype.includes()
该方法可用于判断数组中是否包含某个元素。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 判断数组中是否包含元素 0 const includesZero = int8Array.includes(0);
12. Int8Array.prototype.indexOf()
该方法可用于查找数组中指定元素的第一个下标。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 查找元素 0 在数组中的下标 const index = int8Array.indexOf(0);
13. Int8Array.prototype.join()
该方法可用于将数组中的所有元素连接为一个字符串。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 将数组中的所有元素连接成一个由逗号分隔的字符串 const str = int8Array.join(',');
14. Int8Array.prototype.lastIndexOf()
该方法可用于查找数组中指定元素的最后一个下标。
const int8Array = new Int8Array([-10, -2, 0, 2, 10, 2]); // 查找元素 2 在数组中的最后一个下标 const lastIndex = int8Array.lastIndexOf(2);
15. Int8Array.prototype.map()
该方法可用于对数组中的每个元素进行操作,并返回一个新的数组。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 将数组中的所有元素求平方,得到一个新的数组 const squaredArray = int8Array.map(x => x ** 2);
16. Int8Array.prototype.reduce()
该方法可用于对数组中的所有元素进行累加或其他类似的操作,并返回一个累加后的值。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 对数组中的所有元素求和 const sum = int8Array.reduce((acc, cur) => acc + cur);
17. Int8Array.prototype.reduceRight()
该方法类似于 reduce() 方法,但是遍历数组的顺序是从右往左。
18. Int8Array.prototype.reverse()
该方法可用于将数组中的所有元素翻转。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 将数组中的所有元素翻转 int8Array.reverse();
19. Int8Array.prototype.set()
该方法可用于将一个数组中的元素复制到另一个 Int8Array 数组中。
const int8Array1 = new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]); const int8Array2 = new Int8Array(8); // 将 int8Array1 数组中的所有元素复制到 int8Array2 中 int8Array2.set(int8Array1);
20. Int8Array.prototype.slice()
该方法可用于创建一个新的 Int8Array 数组,该数组中包含了旧数组中指定范围内的元素。
const int8Array1 = new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]); // 创建一个包含 int8Array1 数组中下标为 2~5 的元素的新数组 const int8Array2 = int8Array1.slice(2, 6);
21. Int8Array.prototype.some()
该方法可用于判断数组中是否有符合条件的元素。
const int8Array = new Int8Array([-10, -2, 0, 2, 10]); // 判断数组中是否包含大于 5 的元素 const hasGreaterThan5 = int8Array.some(x => x > 5);
22. Int8Array.prototype.sort()
该方法可用于对数组中的元素进行排序。
const int8Array = new Int8Array([2, 5, 3, 1, 4]); // 将数组中的元素升序排列 int8Array.sort((a, b) => a - b); // 将数组中的元素降序排列 int8Array.sort((a, b) => b - a);
23. Int8Array.prototype.subarray()
该方法可用于创建一个新的 Int8Array 数组,该数组与旧数组共享同一个 ArrayBuffer。
const int8Array1 = new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]); // 创建一个包含 int8Array1 数组中下标为 2~5 的元素的新数组,与 int8Array1 共享 ArrayBuffer const int8Array2 = int8Array1.subarray(2, 6);
总结
本篇文章详细介绍了 Int8Array 类型及其方法,包括了数组的创建、访问、修改、方法的使用以及示例代码。
Int8Array 类型的引入使得 JavaScript 的类型支持更加丰富,也为开发者提供了更多的选择和可能性。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6474751e968c7c53b01d53b4