0
Good morning, that’s my first question around here
I’m making an angled app using the google Books api and I need help.. I have a button that adds indices from the book array to the bookmark array, but I need to prevent the same book from being added to bookmarks, follow the code My question is about the condition I should use in the if case the book already exists in the favorites
books = []
favs = []
addFav(i: number) {
const title = this.books[i].volumeInfo.title
if(???) {
this.toastr.error('Este livro já consta nos seus favoritos!')
} else {
this.favs.push(this.books[i])
this.toastr.success(`O livro "${title}" foi adicionado aos seus favoritos!`)
console.log(this.favs)
}
}
use the
find
of the array object to verify this. Here on the site you have several questions about this, see the one that will help you: https://answall.com/q/363949/57220– Ricardo Pontual
Thanks buddy, I’ll take a look
– Yuri Maciel
You can also use a set that avoids duplicates
– Eduardo Vargas
const exists = this.favs.find(i => i.volumeInfo.title == title) I did this and passed the exists inside the if, it worked! thank you very much
– Yuri Maciel
good that’s right ;)
– Ricardo Pontual