2

Right now, I achieve it by

if (obj instanceof Uint8Array || obj instanceof Uint16Array || obj instanceof Uint32Array ......

Its a very long line. I know that testing instanceof against the parent class of an object yields true so is there a parent class among those typed arrays? I tried TypedArray but according to chrome it doesn't exist.

Anyone know if there is a base class between the typed arrays? Or some other method?

4
  • 1
    Does this answer your question? How do you check if a variable is an array in JavaScript? Commented Jan 21, 2021 at 9:06
  • 1
    @vincenzopalazzo At a glance, none of the answers on that page discuss distinguishing between typed arrays and normal arrays, which is what is being asked for here. Commented Jan 21, 2021 at 9:12
  • Ops! you have right! I jump the typed array, do you have to try with a map? store all the possible type inside a map and make a get by type? you can get the name of the class with myObj.constructor.name Commented Jan 21, 2021 at 9:17
  • 1
    Comment rather than answer, because I don't know quite enough to get it working: According to MDN, all TypedArrays inherit from a TypedArray prototype, which has no global variable but you can get as Object.prototypeOf(UInt8Array). If you can get the appropriate level of prototype from an instance, you can compare against that. Commented Jan 21, 2021 at 9:24

3 Answers 3

4

The TypedArray prototype does exist but is not exposed directly. You can get it using Object.getPrototypeOf from any of its typed arry subclasses, like this:

>> const TypedArray = Object.getPrototypeOf(Uint8Array)
>> console.log((new Uint32Array()) instanceof TypedArray)
true

So with the above const TypedArray in scope, your check simply becomes:

if (obj instanceof TypedArray) { ... }
Sign up to request clarification or add additional context in comments.

1 Comment

It's in the ECMAScript spec so it's not going anywhere!
0

I know this is not a very good solution but I think you can achieve the same kind of behavior using a hacky way. Actually, I don't like this kind of solution. I used Uint8Array.__proto__, I have no idea why the prototype of Uint arrays doesn't exist in chrome.

const uint8Array = new Uint8Array();
const uint16Array = new Uint16Array();
const uint32Array = new Uint32Array();

console.log(uint8Array instanceof Uint8Array.__proto__); //true
console.log(uint16Array instanceof Uint8Array.__proto__); //true
console.log(uint32Array instanceof Uint8Array.__proto__); //true

Edit:

const typedArrays = [new Uint8Array(), new Uint16Array(), new Uint32Array()];

const result = typedArrays.every(elem => elem instanceof Uint8Array.__proto__);

console.log(result);

So, you can check any typed array using Uint8Array.__proto__.

Edit 2:

const isTypedArray = (arg) => {
  return arg instanceof Uint8Array.__proto__;
};


console.log(isTypedArray(uint8Array));// true
console.log(isTypedArray(uint16Array));// true
console.log(isTypedArray(uint32Array));// true

2 Comments

This is still looking at the individual types of typed array. I think the question is looking for a single line that will detect all typed arrays.
@IMSoP Sorry, I'm not so fluent in English. So, I don't know how to explain it but I edited the answer with sample codes. If you understand what I'm saying, please edit my answer, so everyone can understand
0

How about shorten it by checking if it not a normal array like this :

if (!(obj instanceof Array)) {
  // Do stuff with typed array
}

1 Comment

But then what if obj is 42, or new NotAnArrayAtAll()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.