- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
要测试一个对象是否是一个数组,有一个方法——
const myarray = [1,2,3,4];
Array.isArray(myarray);
//returns true
对于类型化数组(例如,Uint8Array)是否有类似的方法?
const int8 = new Uint8Array([0,1,2,3]);
Array.isArray(int8); //returns false
如何确定一个对象是否是类型化数组,以及类型化数组的类型?
最佳答案
您可以对其构造函数1进行比较:
const int8 = new Uint8Array([0,1,2,3]);
console.log(int8.constructor === Uint8Array);
// so you can do
function checkTypedArrayType(someTypedArray) {
const typedArrayTypes = [
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array,
BigInt64Array,
BigUint64Array
];
const checked = typedArrayTypes.filter(ta => someTypedArray.constructor === ta);
return checked.length && checked[0].name || null;
}
console.log(checkTypedArrayType(int8));
// but this can be hugely simplified
function checkTypedArrayType2(someTypedArray) {
return someTypedArray &&
someTypedArray.constructor &&
someTypedArray.constructor.name ||
null;
}
console.log(checkTypedArrayType2(int8));
// which can actually be generalized to
const whatsMyType = someObject =>
someObject &&
someObject.constructor &&
someObject.constructor.name &&
someObject.constructor.name ||
null;
console.log(whatsMyType(int8));
console.log(whatsMyType([1,2,3,4]));
console.log(whatsMyType("hello!"))
console.log(whatsMyType(/[a-z]/i))
1 请注意,没有任何版本的 Internet Explorer 支持 name
属性。 See MDN . See also
关于javascript - 如何找到 typedArray 的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58280379/
C# 有一个高性能的数组复制函数来复制数组就地: Array.Copy(source, destination, length) 它比手动操作更快,即: for (var i = 0; i < len
我正在学习泛型,并用编译器解决了这个问题: type FloatArray = Float32Array | Float64Array; type IntegerArray = | Int8Arr
查看 TypedArray ( link ) 的源代码,我似乎无法弄清楚这两种方法之间的区别。 getInt() 与getInteger() 基本相同,只是在最后加了一个我看不懂的小东西。谁能给我解释
sdk更新到Android 5.0后方法消失了TextView.getTextColor(Context context, TypedArray typedArray, int defStyle) .
为什么 TypedArray 不如常规数组快?我想存储一些预先计算的整数值,并且需要尽可能快地访问数组。 http://jsperf.com/array-access-speed-2/2 准备代码:
我正在尝试使用 WebGL,并希望将一些不同的类型混合到一个字节缓冲区中。我知道 TypedArrays 可以达到这个目的,但不清楚我是否可以与它们混合类型(OpenGL 顶点数据通常是与无符号字节或
我正在学习自定义组件,我在使用自定义 xml 属性时遇到了一些问题。 我的自定义组件扩展了 LinearLayout 并在构造函数中(public Custom(Context context, At
This answer告诉我调用 recycle() TypedArray 的方法允许它被垃圾收集。我的问题是为什么 TypedArray 特别需要一种方法来对其进行垃圾回收?为什么它不能像普通对象一
在 Chrome(可能还有其他浏览器)中,当我将值存储到超出其范围的 TypedArray 时,它会被 chop : const arr = new Uint16Array(1); arr[0] =
我试图用这个简单的代码片段将 typedArray 拆分成更小的 block : const buf = new Uint8Array([0x02, 0x00, 0x07, 0x63, 0x6f, 0
我有一个源 Float32Array,我从中创建了一个辅助 Float32Array。我有一个值序列 model,我想将其作为重复序列复制到辅助 Float32Array 中。我目前正在使用反向 wh
要测试一个对象是否是一个数组,有一个方法—— const myarray = [1,2,3,4]; Array.isArray(myarray); //returns true 对于类型化数组(例如,
在尝试为自己构建一个 WebGL 3D 库时(主要是学习目的),我遵循了从各种来源找到的文档,这些文档指出 TypedArray 函数 set() (专门针对 Float32Array ),在 C 中
我有一个 typedArray,其中包含我想在 ListView 中使用的图标。当我尝试在 for 循环 this.navDrawerIcons 中引用 typedArray 中包含的元素时如下所示,
给定像 Uint8Array 这样的类型化数组,似乎有两种方法可以通过 worker 传输它们。 选项 1 直接发送缓冲区并在接收端进行转换: 发件人:postMessage({fooBuffer:
根据the documentation on the Uint8ClampedArray , The Uint8ClampedArray typed array represents an array
我在 android 自定义 TextView 中设置了多个标志属性,如何使用 TypedArray 恢复这些属性 最佳答案 attrs.xml
我从使用 android L 预览版的设备收到一些崩溃报告,问题是 Caused by: java.lang.RuntimeException: [17, ...... ] recycled twic
我正在尝试编写一个函数,通过将任意 TypedArray 作为输入来扩展/缩小 TypedArray,并返回一个具有不同大小的新的相同类型的 TypedArray,并将原始元素复制到其中。 例如,当您
我正在通过 XHR 加载远程资源并将其接收为 arrayBuffers 。底层数据为float32依赖。我需要对这些数组缓冲区进行一些进一步的操作,例如串联和合并。对于这些操作,我正在尝试 buffe
我是一名优秀的程序员,十分优秀!