Reputation: 1162
I have an object like this:
MyLibrary {
books:
[ Book {
bookType: 'romance'
}
]
}
Book is a class.
I would like to write a test to check if books contain an object that is an instance of Book. I was expecting to use something like MyLibrary.books.indexOf('Book') but it returns -1 even when Book exist.
Upvotes: 2
Views: 1739
Reputation: 10366
You literally have instanceof
:
var hasBook = books.some((book) => book instanceof Book);
Upvotes: 6