亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嵌套多個過濾器方法javascript

嵌套多個過濾器方法javascript

九州編程 2022-05-26 10:27:28
我正在嘗試完成一個挑戰并且我卡住了:我有一個大電影數組,其中有一個屬性類型名稱,這是另一個數組。我想通過從存儲在數組selectedGenres中的復選框中檢查其流派名稱來過濾這部電影 我的困難出現在這里,當我必須檢查每個流派名稱 數組與selectedGenres中的所有元素時。任務是,如果selectedGenres中有多個流派,則僅顯示具有所有這些選定流派的電影,而不僅僅是其中一個。代碼:const movies = [ {title:"movie 1",genreName:["Action", "Drama"]},{title:"movie 2",genreName:["Action", "Fantasy", "Adventure"]},{title:"movie 3",genreName:["Action", "Fantasy", "Science Fiction"]},{title:"movie 4",genreName:["Action", "Drama", "Thriller"]}]const selectedGenres = ["Action","Drama"];  //based on this selection, only movie1 and movie4 should be in the filteredMovies variable.const filteredMovies = movies.filter(movie => { //loop through each movie        movie.genreName.filter(genre => { // loop through each item in genreName array          return selectedGenres.forEach(name => { // loop through each item in selectedGenres array            return name.indexOf(genre) > -1;          });        });      });
查看完整描述

2 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

您正在尋找every


const movies = [

    {title:"movie 1",genreName:["Action", "Drama"]},

    {title:"movie 2",genreName:["Action", "Fantasy", "Adventure"]},

    {title:"movie 3",genreName:["Action", "Fantasy", "Science Fiction"]},

    {title:"movie 4",genreName:["Action", "Drama", "Thriller"]}

];


const filter = ['Action', 'Drama'];


const result = movies.filter(m => filter.length === m.genreName.length && m.genreName.every(g => filter.includes(g)));

console.log(result);


查看完整回答
反對 回復 2022-05-26
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

用于filter電影并返回選擇類型true的電影。every


const movies = [ 

{title:"movie 1",genreName:["Action", "Drama"]},

{title:"movie 2",genreName:["Action", "Fantasy", "Adventure"]},

{title:"movie 3",genreName:["Action", "Fantasy", "Science Fiction"]},

{title:"movie 4",genreName:["Action", "Drama", "Thriller"]}

]

const selectedGenres = ["Action","Drama"];  //based on this selection, only movie1 and movie4 should be in the filteredMovies variable.



const filteredMovies = movies.filter(movie => selectedGenres.every(filter => movie.genreName.includes(filter)))


console.log(filteredMovies);


查看完整回答
反對 回復 2022-05-26
  • 2 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號