[英]Filter List of Object-Nested Objects with Object Containing Key-Array Pair
我試圖想出一個最具描述性的標題。
我想出了一個解決方案,但它的效率非常低,因為它的順序是 O(n^4) 左右,這很可悲。
我正在嘗試根據過濾器對象和匹配的鍵值過濾並返回對象列表。
我將有一個電影列表movieList
,並且我將返回滿足包含在movieFilter
object 中的所有鍵值對的所有電影。 在genre
的情況下,電影必須是流派之一。 由於銷售額不在過濾器中,因此我們不會根據它進行過濾。
let movieFilter = {
"genre": ["Action", "Romance"],
"director": "Bob",
"writer": ["Jim", "Dave", "Alice"],
"duration": "2"
}
let movie = {
"genre" : "Action",
"people": {
"director": "Bob",
"writer": "Alice"
}
"boxoffice": "9,000,000",
"duration": "2"
}
let movies = [{
"genre" : "Action",
"people": {
"director": "Bob",
"writer": "Alice"
}
"boxoffice": "9,000,000",
"duration": "2"
},
{
"genre" : "Comedy",
"people": {
"director": "Rose",
"writer": "Mike"
}
"boxoffice": "12,000,000",
"duration": "3"
}]
我遇到的問題是,因為一些過濾器(如genre
過濾器)在 arrays 中,而一些電影鍵(如people
)嵌套在 object 中,我被迫在循環中嵌套循環,這使得時間復雜度增加。
我可以改變過濾器的結構方式,但它目前的方式對我來說是有意義的。 電影 object 格式是我無法更改的。
任何關於我如何解決這個問題的建議將不勝感激。
可以使用filter
方法和some
方法來完成:
let movieFilter = { "genre": ["Action", "Romance"], "director": "Bob", "writer": ["Jim", "Dave", "Alice"], "duration": "2" } let movies = [ { "genre": "Action", "people": { "director": "Bob", "writer": "Alice" }, "boxoffice": "9,000,000", "duration": "2" }, { "genre": "Comedy", "people": { "director": "Rose", "writer": "Mike" }, "boxoffice": "12,000,000", "duration": "3" }] const result = movies.filter(f => movieFilter.genre.some(ge=> ge == f.genre) && movieFilter.director == f.people.director && movieFilter.writer.some(wr => wr == f.people.writer) && movieFilter.duration == f.duration ); console.log(result);
更新:
感謝@MkeSpaGuy 評論。 我已經重新設計了一些過濾您的項目的方法。
略優化版本,使用key
檢查過濾條件是否滿足:
let movieFilter = { "genre": ["Action", "Romance"], "director": "Bob", "writer": ["Jim", "Dave", "Alice"], "duration": "2" } movieFilter.genre = movieFilter.genre.reduce((a, key) => ({...a, [key]: 1}), {}); movieFilter.writer = movieFilter.writer.reduce((a, key) => ({...a, [key]: 1}), {}); let movies = [{ "genre": "Action", "people": { "director": "Bob", "writer": "Alice" }, "boxoffice": "9,000,000", "duration": "2" }, { "genre": "Comedy", "people": { "director": "Rose", "writer": "Mike" }, "boxoffice": "12,000,000", "duration": "3" }]; const result = movies.filter(f => movieFilter.genre[f.genre] && movieFilter.director == f.people.director && movieFilter.writer[f.people.writer] && movieFilter.duration == f.duration ); console.log(result);
考慮用你的movieFilter中的對象替換你的arrays。
let movieFilter = { genre: { Action: true, Romance: true }, director: "Bob", writer: { Jim: true, Dave: true, Alice: true }, duration: "2" }; let movies = [ { genre: "Action", people: { director: "Bob", writer: "Alice" }, boxoffice: "9,000,000", duration: "2" }, { genre: "Comedy", people: { director: "Rose", writer: "Mike" }, boxoffice: "12,000,000", duration: "3" } ]; const filterOnMovieFilter = movie => { const { genre, people, duration } = movie; const { writer, director } = people || {}; return ( movieFilter.genre[genre] && movieFilter.writer[writer] && movieFilter.duration === duration && movieFilter.director === director ); }; console.log(movies.filter(filterOnMovieFilter));
但是,請避免過早優化。 在現代瀏覽器中過濾 10,000 個項目時,您不會注意到優化。
(注意我說的是過濾而不是顯示)我經常做一些效率較低的事情,以支持簡單、簡潔、不可變、可讀和有用的代碼。 如果您的目標是功能手機或發現真正的性能瓶頸,則需要使用性能監控工具進行測量。 JavaScript 引擎優化不斷改進,結果往往令人驚訝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.