我有一个对象数组:
const books = [
{
title: 'Book',
author: 'Name'
},
{
title: 'Book2',
author: 'Name2'
}
];
Run Code Online (Sandbox Code Playgroud)
我想使用过滤方法将标题提取到数组中。到目前为止,我尝试了这个,但数组返回了 2 个原始对象:
const getTheTitles = function(array) {
const filteredArray = array.filter(function(book) {
return book.title;
})
return filteredArray;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个,但它会导致一个空数组(不知道为什么):
const getTheTitles = function(array) {
const filteredArray = array.filter(function(book) {
book.title;
})
return filteredArray;
}
Run Code Online (Sandbox Code Playgroud)
我知道这可以使用地图来完成。但我正在尝试使用过滤器来完成它。