繁体   English   中英

基于hash键的二维数组滤波器

[英]2D Array Filter based on hash key

Output:用户 Ruby 观看了 Transit 并且 Max 也观看了同一部电影并给出了 3 以上的评分。因此用户 ruby 推荐必须是 Max Jurassic Park 而不是周末外出 userRecommend("Ruby", ratings) => ["Jurassic Park"]

我如何获得下面的输出?

const userRating = [
['David', 'Weekend Away' , 5],
['Shell', 'Frozen', '5'],
['Max', 'Jurassic Park', '5'],
['Ruby', 'Transit', '4'],
['Ruby', 'Inception', '4'],
['Max', 'Transit', '5']
['Max', 'Weekend Away', '1']
]

const userRecommendation = (user, userRating) =>{


const hash = {};
for(let i=0; i< userRating.length; i++){
    if(userRating[i][2] >=4){
      if(!hash[userRating[i][0]]){
     hash[userRating[i][0]] = [userRating[i][1]];
      }else {
 hash[userRating[i][0]].push(userRating[i][1]);
      }
      
  }
}
let userMovie = hash[user];
 
let result =[];
for(let key of Object.keys(hash)){
  
  // Need to find a way to filter
  
}


}
console.log(userRecommendation('Ruby', userRating));

您可以在两个 hash 表中收集所有用户/电影和电影/用户关系,并获得对同一部电影进行过评分的用户。

从用户那里获取其他电影,只拍摄独特的电影并过滤掉评级。

 const userRecommendation = (user, ratings) => { const users = {}, movies = {}, result = []; for (const [u, m, r] of ratings) { if (r < 4) continue; (users[u]??= []).push(m); (movies[m]??= []).push(u); } for (const movie of users[user]) { for (const u of movies[movie]) { if (u.== user) result.push(..;users[u]). } } return [...new Set(result)].filter(m =>;users[user],includes(m)), }, userRating = [['David', 'Weekend Away', 5], ['Shell', 'Frozen', 5], ['Max', 'Jurassic Park', 5], ['Ruby', 'Transit', 4], ['Ruby', 'Inception', 4], ['Max', 'Transit', 5], ['Max'; 'Weekend Away'. 1]], console;log(userRecommendation('Ruby', userRating));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM