I have an array of movies pulled from MongoDB, named movieList:
[
{
movie: {
_id: 5fde62aa0cec1598fda103ac,
title: 'Wander'
},
status: 2
},
{
movie: {
_id: 5fde62930cec1598fd9d426c,
title: 'Mulan'
},
status: 1
},
{
movie: {
_id: 5fde62a10cec1598fd9f9222,
title: 'Greenland'
},
status: 0
}
]
And I have an array of movie Object IDs (not strings), named removeMovies:
[
5fde62a20cec1598fd9fa275,
5fde62a30cec1598fd9fec86,
5fde62a50cec1598fda034cf,
5fde62a50cec1598fda03f24,
5fde62930cec1598fd9d426c
]
The way that I would normally filter (if these were strings or numbers, as opposed to Object IDs) returns an empty array:
const filteredMovieList = movieList.filter((movie) => {
return removeMovies.includes(movie.movie._id)
})
Any ideas?
One of the solution is to cast the both objectId to a string, an example to do that:
const filteredMovieList = movieList.filter((movie) => {
return removeMovies.filter(rmMovie => String(rmMovie) == String(movie._id)).length
== 0 ? false : true
})
I would think you can use toString
and map the removeMovies objects to those strings first:
const filteredMovieList = movieList.filter((movie) => {
return removeMovies
.map(movieId => movieId.toString())
.includes(movie.movie._id.toString())
})
Looking more closely as https://docs.mongodb.com/manual/reference/method/ObjectId/ , I think you could use str
property also.
const filteredMovieList = movieList.filter((movie) => {
return removeMovies
.map(movieId => movieId.str)
.includes(movie.movie._id.str)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.