Please help! How to create a function to Find books with an author that has < 4 characters?
const books = [
{
"id": "a5b16ad1bb2e96b8c649da7150ad5726",
"title": "Mon Oct 17 2022 21:32:42 GMT-0700 (Pacific Daylight Time)",
"author": "Ana"
},
{
"id": "221517d1a0948a2faa56a824803aa60e",
"title": "bjlci",
"author": "Lea Ann"
},
{
"id": "286332e30d3962f5de20c2cf8b673482",
"title": "Mon Oct 24 2022 00:38:43 GMT-0700 (Pacific Daylight Time)",
"author": "Ana Lee"
},
{
"id": "bd78bed71d601bdd19c76c8309287894",
"title": "Mon Oct 17 2022 21:32:42 GMT-0700 (Pacific Daylight Time)",
"author": "Lee"
}
]
This is what I have so far: it gives me erroe message TypeError: Cannot read properties of undefined (reading 'length')
function shortNameAuthors(books){
for (let i = 0; i < books.length; i ){
if (books["author"].length <4){
console.log(books[i]);
}
}
}
console.log (shortNameAuthors(books))
CodePudding user response:
function shortNameAuthors(books) {
const filteredBooks = books.filter(
book => book.author.length < 4
)
return filteredBooks
}