I have a booklist and try to print out those already read in ‘title by author’ form
const bookList = [
{title: "Harry Potter", author: "J.K. Rowling", alreadyRead: true},
{title: "Never Eat Alone", author: "Keith Ferrazzi", alreadyRead: false},
{title: "INSPIRED", author: "Marty Cagan", alreadyRead: false},
{title: "Zero to One", author: "Peter Thiel", alreadyRead: true}
]
at first I use this for loop method by passing the booklist and status to call the function printBookList(bookList, true)
but it end up printing all the elements in the list
function printBookList(bookList, completedReadingBook){
for(i=0;i<bookList.length;i++){
if (bookList[i].alreadyRead == completedReadingBook) {
console.log(bookList[i].title + ' by ' + bookList[i].author);
}
}
}
then I also try with filter()
method as below but it end up showing empty
for(i=0; i<bookList.length;i++){
let filtered = bookList.filter((readbook)=>readbook.alreadyRead==completedReadingBook)
return filtered;
}
console.log(bookList[filtered].title + ' by ' + bookList[filtered].author);
I’m quite new to this js, will be much appreciate if someone could explain this to me, thx a lot!