如何 return 具有给定值的对象?
How to return an object with a given value?
我正在做一个搜索过滤器。我需要 return 包含用户给定演员的整个对象。我可以找到演员,但我不确定如何 return 演员所在的对象。
JSON 文件:
{
"title": "American Assassin",
"year": 2017,
"cast": [
"Dylan O'Brien",
"Michael Keaton",
"Sanaa Lathan",
"Shiva Negar",
"Taylor Kitsch"
],
"genres": []
},
{
"title": "Mother!",
"year": 2017,
"cast": [
"Jennifer Lawrence",
"Javier Bardem",
"Michelle Pfeiffer",
"Domhnall Gleeson",
"Ed Harris",
"Kristen Wiig"
],
"genres": []
}
我的过滤函数:
checkCast(allMovies) {
return filter(allMovies, (movie) => {
const actor = filter(movie.cast, (actor) => {
return actor.toLowerCase().includes(this.search.cast.toLowerCase())
})
console.log(actor)
return actor
});
}
希望一切都清楚。
您可以使用 array#find
和 array#includes
来检查电影演员表是否包含演员。
const movies = [{ "title": "American Assassin", "year": 2017, "cast": [ "Dylan O'Brien", "Michael Keaton", "Sanaa Lathan", "Shiva Negar", "Taylor Kitsch" ], "genres": [] }, { "title": "Mother!", "year": 2017, "cast": [ "Jennifer Lawrence", "Javier Bardem", "Michelle Pfeiffer", "Domhnall Gleeson", "Ed Harris", "Kristen Wiig" ], "genres": [] }],
actor = 'Ed Harris',
movie = movies.find(({cast}) => cast.includes(actor));
console.log(movie);
您也可以将演员和演员姓名转换为小写,以便使用 array#find
和 array#some
进行比较。
const movies = [{ "title": "American Assassin", "year": 2017, "cast": [ "Dylan O'Brien", "Michael Keaton", "Sanaa Lathan", "Shiva Negar", "Taylor Kitsch" ], "genres": [] }, { "title": "Mother!", "year": 2017, "cast": [ "Jennifer Lawrence", "Javier Bardem", "Michelle Pfeiffer", "Domhnall Gleeson", "Ed Harris", "Kristen Wiig" ], "genres": [] }],
actor = 'ed harris',
movie = movies.find(({cast}) => cast.some(member => member.toLowerCase() === actor.toLowerCase()));
console.log(movie);
您需要通过在 case
数组中查找演员来过滤 allMovies
数组。如果找到匹配的演员,请使用 Array.some()
(或 lodash _.some()
)到 return true
。
const search = { cast: 'bardem' };
function checkCast(allMovies) {
return allMovies.filter(movie =>
movie.cast.some(actor =>
actor.toLowerCase().includes(search.cast.toLowerCase())
)
);
}
const allMovies = [{"title":"American Assassin","year":2017,"cast":["Dylan O'Brien","Michael Keaton","Sanaa Lathan","Shiva Negar","Taylor Kitsch"],"genres":[]},{"title":"Mother!","year":2017,"cast":["Jennifer Lawrence","Javier Bardem","Michelle Pfeiffer","Domhnall Gleeson","Ed Harris","Kristen Wiig"],"genres":[]}]
const result = checkCast(allMovies)
console.log(result)
我正在做一个搜索过滤器。我需要 return 包含用户给定演员的整个对象。我可以找到演员,但我不确定如何 return 演员所在的对象。
JSON 文件:
{
"title": "American Assassin",
"year": 2017,
"cast": [
"Dylan O'Brien",
"Michael Keaton",
"Sanaa Lathan",
"Shiva Negar",
"Taylor Kitsch"
],
"genres": []
},
{
"title": "Mother!",
"year": 2017,
"cast": [
"Jennifer Lawrence",
"Javier Bardem",
"Michelle Pfeiffer",
"Domhnall Gleeson",
"Ed Harris",
"Kristen Wiig"
],
"genres": []
}
我的过滤函数:
checkCast(allMovies) {
return filter(allMovies, (movie) => {
const actor = filter(movie.cast, (actor) => {
return actor.toLowerCase().includes(this.search.cast.toLowerCase())
})
console.log(actor)
return actor
});
}
希望一切都清楚。
您可以使用 array#find
和 array#includes
来检查电影演员表是否包含演员。
const movies = [{ "title": "American Assassin", "year": 2017, "cast": [ "Dylan O'Brien", "Michael Keaton", "Sanaa Lathan", "Shiva Negar", "Taylor Kitsch" ], "genres": [] }, { "title": "Mother!", "year": 2017, "cast": [ "Jennifer Lawrence", "Javier Bardem", "Michelle Pfeiffer", "Domhnall Gleeson", "Ed Harris", "Kristen Wiig" ], "genres": [] }],
actor = 'Ed Harris',
movie = movies.find(({cast}) => cast.includes(actor));
console.log(movie);
您也可以将演员和演员姓名转换为小写,以便使用 array#find
和 array#some
进行比较。
const movies = [{ "title": "American Assassin", "year": 2017, "cast": [ "Dylan O'Brien", "Michael Keaton", "Sanaa Lathan", "Shiva Negar", "Taylor Kitsch" ], "genres": [] }, { "title": "Mother!", "year": 2017, "cast": [ "Jennifer Lawrence", "Javier Bardem", "Michelle Pfeiffer", "Domhnall Gleeson", "Ed Harris", "Kristen Wiig" ], "genres": [] }],
actor = 'ed harris',
movie = movies.find(({cast}) => cast.some(member => member.toLowerCase() === actor.toLowerCase()));
console.log(movie);
您需要通过在 case
数组中查找演员来过滤 allMovies
数组。如果找到匹配的演员,请使用 Array.some()
(或 lodash _.some()
)到 return true
。
const search = { cast: 'bardem' };
function checkCast(allMovies) {
return allMovies.filter(movie =>
movie.cast.some(actor =>
actor.toLowerCase().includes(search.cast.toLowerCase())
)
);
}
const allMovies = [{"title":"American Assassin","year":2017,"cast":["Dylan O'Brien","Michael Keaton","Sanaa Lathan","Shiva Negar","Taylor Kitsch"],"genres":[]},{"title":"Mother!","year":2017,"cast":["Jennifer Lawrence","Javier Bardem","Michelle Pfeiffer","Domhnall Gleeson","Ed Harris","Kristen Wiig"],"genres":[]}]
const result = checkCast(allMovies)
console.log(result)