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 ?
asked Aug 26, 2021 at 8:29
1 Answer 1
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));
answered Aug 26, 2021 at 8:38
Explore related questions
See similar questions with these tags.
lang-js
check if this array is instanceof Animal objects
Do you meancheck if this array only contains instanceof Animal objects
?