Check if object is Array of instance of specific object in Node.JS

I have a simple class in Node JS

class Animal {
    constructor(name) {
      this.name = name;
      // more fields ...
    }
}

let objects = [];
objects.push(new Animal('Dog'));
objects.push(new Animal('Cat'));
objects.push(new Animal('Dinosaur'));
console.log(objects);

What’s the best way to check if this array is instanceof Animal objects , without looping over and checking instanceof of each element ?

Answer

Use Array.prototype.every():

class Animal {
    constructor(name) {
      this.name = name;
      // more fields ...
    }
}

let objects = [];
objects.push(new Animal('Dog'));
objects.push(new Animal('Cat'));
objects.push(new Animal('Dinosaur'));
console.log(objects.every(x => x instanceof Animal));