第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

嵌套多個過濾器方法javascript

嵌套多個過濾器方法javascript

九州編程 2022-05-26 10:27:28
我正在嘗試完成一個挑戰(zhàn)并且我卡住了:我有一個大電影數(shù)組,其中有一個屬性類型名稱,這是另一個數(shù)組。我想通過從存儲在數(shù)組selectedGenres中的復(fù)選框中檢查其流派名稱來過濾這部電影 我的困難出現(xiàn)在這里,當(dāng)我必須檢查每個流派名稱 數(shù)組與selectedGenres中的所有元素時。任務(wù)是,如果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條經(jīng)驗 獲得超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);


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

TA貢獻1804條經(jīng)驗 獲得超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);


查看完整回答
反對 回復(fù) 2022-05-26
  • 2 回答
  • 0 關(guān)注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號