Detect if object is either an Array or typed array
javascript, typed-arrays
Solution
function isArrayOrTypedArray(x) {
return Boolean(x && (typeof x === 'object') && (Array.isArray(x) || (ArrayBuffer.isView(x) && !(x instanceof DataView)));
}
Problem
I need to determine whether a given object is either an `Array`, or typed array such as `Float32Array`. Currently I'm checking whether the `.length` property is defined, but this isn't always indicative of an array. Similar issues arise with existence checking of `.forEach()` or other methods. Several `instanceof` checks would suffice, as done here - but I'm wondering if there is a simple built-in feature, e.g., a generic `Array.isArray()` function that does what I need.