javascript Uint8Array

Uint8Array表示8bit unsigned integers向量,初始化为0

构造

new Uint8Array(); // new in ES2017
new Uint8Array(length);
new Uint8Array(typedArray);
new Uint8Array(object);
new Uint8Array(buffer [, byteOffset [, length]]);

属性

Uint8Array.prototype.constructor|
Uint8Array.prototype.buffer|
Uint8Array.prototype.byteLength |字节的长度
Uint8Array.prototype.length|元素的长度

方法

方法描述
Uint8Array.prototype.copyWithin()
Uint8Array.prototype.entries()
Uint8Array.prototype.every()
Uint8Array.prototype.fill()
Uint8Array.prototype.filter()
Uint8Array.prototype.find()
Uint8Array.prototype.findIndex()
Uint8Array.prototype.forEach()
Uint8Array.prototype.includes()
Uint8Array.prototype.indexOf()
Uint8Array.prototype.join()
Uint8Array.prototype.keys()
Uint8Array.prototype.lastIndexOf()
Uint8Array.prototype.map()
Uint8Array.prototype.reduce()
Uint8Array.prototype.reduceRight()
Uint8Array.prototype.reverse()
Uint8Array.prototype.set()
Uint8Array.prototype.slice()
Uint8Array.prototype.some()
Uint8Array.prototype.sort()
Uint8Array.prototype.subarray()
Uint8Array.prototype.values()
Uint8Array.prototype.toLocaleString()
Uint8Array.prototype.toString()
Uint8Array.prototype@@iterator

例子

// From a length
var uint8 = new Uint8Array(2);
uint8[0] = 42;
console.log(uint8[0]); // 42
console.log(uint8.length); // 2
console.log(uint8.BYTES_PER_ELEMENT); // 1

// From an array
var arr = new Uint8Array([21,31]);
console.log(arr[1]); // 31

// From another TypedArray
var x = new Uint8Array([21, 31]);
var y = new Uint8Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint8Array(buffer, 1, 4);

// From an iterable 
var iterable = function*(){ yield* [1,2,3]; }(); 
var uint8 = new Uint8Array(iterable); 
// Uint8Array[1, 2, 3]

参考:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
https://www.jb51.net/article/147112.htm
https://developer.51cto.com/art/201704/536718.htm

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript中,`Uint8Array`是一种类型化数组(TypedArray),它用于表示一个包含8位无符号整数的固定长度数组。以下是`Uint8Array`的一些常用用法: 1. 创建一个`Uint8Array`对象: - 使用数组创建:`const arr = new Uint8Array([1, 2, 3, 4, 5]);` - 使用指定长度创建:`const arr = new Uint8Array(5);` 2. 获取`Uint8Array`的长度:`const length = arr.length;` 3. 访问和修改`Uint8Array`的元素: - 通过索引访问:`const value = arr[index];` - 通过索引修改:`arr[index] = newValue;` 4. 将`Uint8Array`对象转换为普通数组: - 使用`Array.from()`方法:`const plainArray = Array.from(arr);` - 使用`spread`运算符:`const plainArray = [...arr];` 5. 将普通数组转换为`Uint8Array`对象:`const arr = new Uint8Array([1, 2, 3, 4, 5]);` 6. 拷贝一个`Uint8Array`对象: - 浅拷贝:`const copy = Uint8Array.from(arr);` - 深拷贝:`const copy = new Uint8Array(arr);` 7. 连接多个`Uint8Array`对象: - 使用`concat()`方法:`const combined = Uint8Array.of(...arr1, ...arr2);` 8. 切片一个`Uint8Array`对象: - 使用`slice()`方法:`const slice = arr.slice(start, end);` 9. 迭代`Uint8Array`对象: - 使用`for...of`循环:`for (const value of arr) { ... }` - 使用`forEach()`方法:`arr.forEach((value, index) => { ... });` 这些是`Uint8Array`的一些常用用法和操作。除了`Uint8Array`,JavaScript还提供了其他类型化数组(如`Int8Array`、`Uint16Array`、`Float32Array`等),可以根据具体的需求选择适合的类型化数组来处理数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值