我有一个对象数组,每个对象都有一个“被监视”的属性,它本身就是一个对象数组。
let arr =
[
{
id: 1,
name: "A",
watched: [
{movieId: 7, movieName: "Avatar"} ,
{movieId: 8, movieName: "Titanic"}
]
},
{
id: 2,
name: "B",
watched: [
{movieId: 1, movieName: "Armageddon"} ,
{movieId: 8, movieName: "Titanic"}
]
},
{
id: 3,
name: "C",
watched: [
{movieId: 1, movieName: "Armageddon"} ,
{movieId: 7, movieName: "Avatar"}
]
}
]
根据给定的 MovieId,我需要一组索引。如果我想要一个索引数组谁看过 movieId 没有。7 (Avatar) - 我会得到:
让 AvatarWatchers = [0,2]
我试过:
AvatarWatchers = arr.filter((item) => item.watched.find(movieObj => movieObj.movieId == 7) > -1 )
但它不起作用,我得到一个空数组。
摇曳的蔷薇
相关分类