2

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.

asked Jul 18, 2017 at 11:52
2
  • 3
    You want to check whether .some elements in books are an instanceof Book... Commented Jul 18, 2017 at 11:55
  • 2
    wow.. -2 already just because I didn't know about .some and instanceof. It worked @deceze thanks! Commented Jul 18, 2017 at 12:00

1 Answer 1

6

You literally have instanceof:

var hasBook = books.some((book) => book instanceof Book);
answered Jul 18, 2017 at 11:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.