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?

user81993
  • 6,167
  • 6
  • 32
  • 64
  • 1
    Does this answer your question? [How do you check if a variable is an array in JavaScript?](https://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript) – vincenzopalazzo Jan 21 '21 at 09:06
  • 1
    @vincenzopalazzo At a glance, none of the answers on that page discuss distinguishing between [_typed_ arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) and [_normal_ arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), which is what is being asked for here. – IMSoP Jan 21 '21 at 09: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` – vincenzopalazzo Jan 21 '21 at 09: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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#description), 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. – IMSoP Jan 21 '21 at 09:24

3 Answers3

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) { ... }
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • [It's in the ECMAScript spec](https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object) so it's not going anywhere! – Thomas Jan 21 '21 at 12:28
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
Thant Sin Aung
  • 682
  • 2
  • 10
  • 20
  • 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 Jan 21 '21 at 09:27
  • @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 – Thant Sin Aung Jan 21 '21 at 09:38
0

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

if (!(obj instanceof Array)) {
  // Do stuff with typed array
}
Ethan Vu
  • 2,911
  • 9
  • 25