如果我在这个结构中有一些数据:
const topFilms = [
{ title: 'The Shawshank Redemption', year: 1994, cast: [{'Al Pacino': false}, {'Morgan Freeman': true}] },
{ title: 'The Godfather', year: 1972, cast: [{'Marlon Brando': true}, {'Al Pacino': true}] },
{ title: 'The Godfather: Part II', year: 1974, cast: [{'Al Pacino': true}, {'Robert De Niro': true}] },
{ title: 'The Dark Knight', year: 2008 }
];
如何创建在其属性中具有特定键值对的新项目数组?例如,我想要一组由 Al Pacino 主演的电影,因此我需要过滤该数组以包含'Al Pacino': true在其属性中具有键值对的对象。此代码不起作用- 这只是我的最佳尝试。
const alPacinoFilms = topFilms.filter(function (film) {
if(film.cast){
return film.cast.includes({'Al Pacino': true});
}
});
CodePen:https ://codepen.io/m-use/pen/ZEEjVOP
繁花如伊
哈士奇WWW
相关分类