繁体   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方法,该方法将返回前三部电影标题的数组,这些电影标题在给定用户的朋友中平均得分最高。
  2. 在寻找收视率最高的电影时,应仅考虑由给定用户的朋友给出的收视率。
  3. 如果没有此类电影,则应返回一个空列表。
  4. 评级相同的电影应按字母顺序排列。

对于userID 62289,输出应为[“低俗小说”,“教父”,“黑暗骑士”]我认为这是一个复杂的嵌套循环,而且我没有得到实际的预期结果,并且我认为迭代在某些地方还存在问题,有人可以纠正我我做错了什么吗?

我认为,根据唯一朋友的平均评分顺序,其输出为[289]“教父”,“黑暗骑士”,“肖申克救赎” [9,8.5,8]。

下面是我的代码。

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