简体   繁体   English

嵌套的复杂json数据访问

[英]Nested complex json data access

 const users = [ { userId: 15291, email: "Constantin_Kuhlman15@yahoo.com", friends: [7001, 51417, 62289] }, { userId: 7001, email: "Keven6@gmail.com", friends: [15291, 51417, 62289, 66380] }, { userId: 51417, email: "Margaretta82@gmail.com", friends: [15291, 7001, 9250] }, { userId: 62289, email: "Marquise.Borer@hotmail.com", friends: [15291, 7001] } ]; const movies = [ { "title": "The Shawshank Redemption", "duration": "PT142M", "actors": [ "Tim Robbins", "Morgan Freeman", "Bob Gunton" ], "ratings": [ { "userId": 7001, "rating": 8 }, { "userId": 9250, "rating": 9 }, { "userId": 34139, "rating": 8 } ], "favorites": [ 66380, 7001, 9250, 34139 ], "watchlist": [ 15291, 51417, 62289, 6146, 71389, 93707 ] }, { "title": "The Godfather", "duration": "PT175M", "actors": [ "Marlon Brando", "Al Pacino", "James Caan" ], "ratings": [ { "userId": 15291, "rating": 9 }, { "userId": 51417, "rating": 9 }, { "userId": 7001, "rating": 9 }, { "userId": 9250, "rating": 7 }, { "userId": 71389, "rating": 9 } ], "favorites": [ 15291, 51417, 7001, 9250, 71389, 93707 ], "watchlist": [ 62289, 66380, 34139, 6146 ] }, { "title": "The Dark Knight", "duration": "PT152M", "actors": [ "Christian Bale", "Heath Ledger", "Aaron Eckhart" ], "ratings": [ { "userId": 15291, "rating": 8 }, { "userId": 7001, "rating": 9 }, { "userId": 9250, "rating": 6 }, { "userId": 34139, "rating": 7 }, { "userId": 93707, "rating": 7 } ], "favorites": [ 15291, 7001, 9250, 34139, 93707 ], "watchlist": [ 51417, 62289, 6146, 71389 ] }, { "title": "Pulp Fiction", "duration": "PT154M", "actors": [ "John Travolta", "Uma Thurman", "Samuel L. Jackson" ], "ratings": [ { "userId": 62289, "rating": 8 }, { "userId": 66380, "rating": 5 }, { "userId": 6146, "rating": 6 }, { "userId": 71389, "rating": 7 } ], "favorites": [ 15291, 51417, 62289, 66380, 71389, 93707 ], "watchlist": [ 7001, 9250, 34139, 6146 ] } ]; // solution class MoviesAnalyzer { constructor(movies, users) { this.movies = movies; this.users = users; } // -- my code topRatedMoviesByFriends(userId) { const user = this.users.filter( user => user.userId === userId )[0]; if (user === undefined) throw 'no such user'; let moviesFriends = {}; for (const movie of this.movies) { let friendsCount = 0; for (const friend of user.friends) { console.log("friend",friend); for (const userId of movie.ratings) { // console.log("userId",userId); if (movie.ratings.includes(userId)) { friendsCount++; } } } if (moviesFriends[friendsCount]) { moviesFriends[friendsCount].push(movie.title); } else if (friendsCount > 0) { moviesFriends[friendsCount] = [movie.title]; } } return moviesFriends; } topRatedMoviesAmongFriends(userId){ const friendsTopRatedlist = this.topRatedMoviesByFriends(userId); let topMovies = []; const sortedKeys = Object.keys(friendsTopRatedlist).sort((a, b) => parseInt(a) < parseInt(b)); for (const key of sortedKeys) { let sortedTitles = friendsTopRatedlist[key].sort(); while (sortedTitles.length > 0 && topMovies.length < 4) { topMovies.push(sortedTitles.shift()); } } return topMovies; } } // test const analyzer = new MoviesAnalyzer(movies, users); console.log(analyzer.topRatedMoviesAmongFriends(62289)); 

  1. topRatedMoviesAmongFriends method that will return an array of top three movie titles, that have the highest average rating among friends of a given user. topRatedMoviesAmongFriends方法,该方法将返回前三部电影标题的数组,这些电影标题在给定用户的朋友中平均得分最高。
  2. When looking for top rated movies, only ratings given by friends of a given user should be considered. 在寻找收视率最高的电影时,应仅考虑由给定用户的朋友给出的收视率。
  3. If there are no such movies, then an empty list should be returned. 如果没有此类电影,则应返回一个空列表。
  4. Movies that have equal rating, should be ordered alphabetically. 评级相同的电影应按字母顺序排列。

for userID 62289, output should be ["pulp fiction", "The Godfather", "The Dark Knight"] I think it's a complex nested loops, And I am not getting the actual expected result and I think iteration is also somewhere wrongs, can someone please correct me that where I am doing wrong? 对于userID 62289,输出应为[“低俗小说”,“教父”,“黑暗骑士”]我认为这是一个复杂的嵌套循环,而且我没有得到实际的预期结果,并且我认为迭代在某些地方还存在问题,有人可以纠正我我做错了什么吗?

I think that the output of 62289 is ["The Godfather", "The Dark Knight", "The Shawshank Redemption"] based on the order of average rating of only friends [9, 8.5, 8]. 我认为,根据唯一朋友的平均评分顺序,其输出为[289]“教父”,“黑暗骑士”,“肖申克救赎” [9,8.5,8]。

Below is my code. 下面是我的代码。

const users = [
  {
    userId: 15291,
    email: "Constantin_Kuhlman15@yahoo.com",
    friends: [7001, 51417, 62289]
    },
  {
    userId: 7001,
    email: "Keven6@gmail.com",
    friends: [15291, 51417, 62289, 66380]
    },
  {
    userId: 51417,
    email: "Margaretta82@gmail.com",
    friends: [15291, 7001, 9250]
    },
  {
    userId: 62289,
    email: "Marquise.Borer@hotmail.com",
    friends: [15291, 7001]
    }
];

const movies = [
  {
    "title": "The Shawshank Redemption",
    "duration": "PT142M",
    "actors": [
            "Tim Robbins",
            "Morgan Freeman",
            "Bob Gunton"
        ],
    "ratings": [
      {
        "userId": 7001,
        "rating": 8
            },
      {
        "userId": 9250,
        "rating": 9
            },
      {
        "userId": 34139,
        "rating": 8
            }
        ],
    "favorites": [
            66380,
            7001,
            9250,
            34139
        ],
    "watchlist": [
            15291,
            51417,
            62289,
            6146,
            71389,
            93707
        ]
    },
  {
    "title": "The Godfather",
    "duration": "PT175M",
    "actors": [
            "Marlon Brando",
            "Al Pacino",
            "James Caan"
        ],
    "ratings": [
      {
        "userId": 15291,
        "rating": 9
            },
      {
        "userId": 51417,
        "rating": 9
            },
      {
        "userId": 7001,
        "rating": 9
            },
      {
        "userId": 9250,
        "rating": 7
            },
      {
        "userId": 71389,
        "rating": 9
            }
        ],
    "favorites": [
            15291,
            51417,
            7001,
            9250,
            71389,
            93707
        ],
    "watchlist": [
            62289,
            66380,
            34139,
            6146
        ]
    },
  {
    "title": "The Dark Knight",
    "duration": "PT152M",
    "actors": [
            "Christian Bale",
            "Heath Ledger",
            "Aaron Eckhart"
        ],
    "ratings": [
      {
        "userId": 15291,
        "rating": 8
            },
      {
        "userId": 7001,
        "rating": 9
            },
      {
        "userId": 9250,
        "rating": 6
            },
      {
        "userId": 34139,
        "rating": 7
            },
      {
        "userId": 93707,
        "rating": 7
            }
        ],
    "favorites": [
            15291,
            7001,
            9250,
            34139,
            93707
        ],
    "watchlist": [
            51417,
            62289,
            6146,
            71389
        ]
    },
  {
    "title": "Pulp Fiction",
    "duration": "PT154M",
    "actors": [
            "John Travolta",
            "Uma Thurman",
            "Samuel L. Jackson"
        ],
    "ratings": [
      {
        "userId": 62289,
        "rating": 8
            },
      {
        "userId": 66380,
        "rating": 5
            },
      {
        "userId": 6146,
        "rating": 6
            },
      {
        "userId": 71389,
        "rating": 7
            }
        ],
    "favorites": [
            15291,
            51417,
            62289,
            66380,
            71389,
            93707
        ],
    "watchlist": [
            7001,
            9250,
            34139,
            6146
        ]
    }
];

// solution
class MoviesAnalyzer {
  constructor(movies, users) {
    this.movies = movies;
    this.users = users;
  }
  // -- my code
  topRatedMoviesByFriends(userId) {

    const user = this.users.filter(
      user => user.userId === userId
    )[0];
    if (user === undefined) throw 'no such user';

    let moviesFriends = {};
    for (const movie of this.movies) {
      let totalRating = 0;
      let friendsCount = 0;
      for (const friend of user.friends) {
        console.log("friend", friend);
        for (const rating of movie.ratings) {
          //  console.log("userId",userId);
          if (rating.userId == friend) {
            totalRating += rating.rating
            friendsCount++;
          }
        }
      }

      console.log(totalRating)
      const averageRating = totalRating / friendsCount
      if (moviesFriends[averageRating]) {
        moviesFriends[averageRating].push(movie.title);
      } else if (averageRating > 0) {
        moviesFriends[averageRating] = [movie.title];
      }
    }
    console.log(moviesFriends)
    return moviesFriends;
  }

  topRatedMoviesAmongFriends(userId) {

    const friendsTopRatedlist = this.topRatedMoviesByFriends(userId);
    let topMovies = [];
    const sortedKeys = Object.keys(friendsTopRatedlist).sort(this.compareRating);
    console.log(sortedKeys)
    for (const key of sortedKeys) {
      let sortedTitles = friendsTopRatedlist[key].sort();
      while (sortedTitles.length > 0 && topMovies.length < 3) {
        topMovies.push(sortedTitles.shift());
      }
    }
    return topMovies;
  }

  compareRating(a, b) {
    const fa = parseFloat(a)
    const fb = parseFloat(b)

    if (fa < fb) return 1
    else if (fa > fb) return -1
    else return 0
  }


}

// test
const analyzer = new MoviesAnalyzer(movies, users);
console.log(analyzer.topRatedMoviesAmongFriends(62289));

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

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