I have an array of objects:
const books = [
{
title: 'Book',
author: 'Name'
},
{
title: 'Book2',
author: 'Name2'
}
];
I'd like to extract the titles into an array using the filter method. So far I tried this but the array returns with the 2 original objects:
const getTheTitles = function(array) {
const filteredArray = array.filter(function(book) {
return book.title;
})
return filteredArray;
}
I also tried this but it results in an empty array (not sure why):
const getTheTitles = function(array) {
const filteredArray = array.filter(function(book) {
book.title;
})
return filteredArray;
}
I understand that this could be accomplished using map. But I am trying to accomplish it using filter.
map, notfilter:const titles = books.map(book => book.title);.filteris for FILTERing not for mapping