繁体   English   中英

Javascript:坚持比较功能

[英]Javascript: stuck with comparison function

我被卡住了,需要帮助完成这个获取请求。 我需要一个函数来检查请求中的电影(单个对象)之前是否已被用户评级。

评级在 ratingMovies 数组中。 如果电影被评级,我需要将 userRating 属性及其值添加到响应中。 如果尚未评级,我需要 userRating 值为 null

 const ratedMovies = useStore((state) => state.ratedMovies)

  const getMovieDetails = () => {
    const key = `https://www.omdbapi.com/?i=${movie.imdbID}&apikey=b46dc190`
    axios
      .get(key)
      .then((response) => {
       

       // Here a comparison is required, to check if the movie in the response (a single object)
       // has ever been rated before and its ID (imdbID) and userRating (property to be added) is        
       // present in the ratedMovies array
       // if it is present I need the property userRating nad it's value to be added to the response
       // or when it is empty, to have the userRating be null

       setModalDetails(response.data)
      })
     
      .then((response) => {
        console.log(modalDetails)
      })
      .catch((error) => console.log(error))
  }

示例 axios 响应:

{
    "Title": "Star Wars: Episode V - The Empire Strikes Back",
    "Year": "1980",
    "Rated": "PG",
    "Released": "20 Jun 1980",
    "Runtime": "124 min",
    "Genre": "Action, Adventure, Fantasy",
    "Director": "Irvin Kershner",
    "Writer": "Leigh Brackett, Lawrence Kasdan, George Lucas",
    "Actors": "Mark Hamill, Harrison Ford, Carrie Fisher",
    "Plot": "After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training with Yoda, while his friends are pursued across the galaxy by Darth Vader and bounty hunter Boba Fett.",
    "Language": "English",
    "Country": "United States, United Kingdom",
    "Awards": "Won 2 Oscars. 25 wins & 20 nominations total",
    "Poster": "https://m.media-amazon.com/images/M/MV5BYmU1NDRjNDgtMzhiMi00NjZmLTg5NGItZDNiZjU5NTU4OTE0XkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg",
    "Ratings": [
        {
            "Source": "Internet Movie Database",
            "Value": "8.7/10"
        },
        {
            "Source": "Rotten Tomatoes",
            "Value": "94%"
        },
        {
            "Source": "Metacritic",
            "Value": "82/100"
        }
    ],
    "Metascore": "82",
    "imdbRating": "8.7",
    "imdbVotes": "1,209,128",
    "imdbID": "tt0080684",
    "Type": "movie",
    "DVD": "21 Sep 2004",
    "BoxOffice": "$292,753,960",
    "Production": "N/A",
    "Website": "N/A",
    "Response": "True"
}

样品评级:

ratedMovies = [{imdbID: 'tt0080684', userRating: 8}]

您可以像这样使用 .filter 函数:

ratedMovies = [{imdbID: 'tt0080684', userRating: 8}]
notratedMovies = [{imdbID: 'tt0080111', userRating: 8}]

let result = ratedMovies.filter(obj => {
  return obj.imdbID === imdb.imdbID
});

console.log(result);

let notresult = notratedMovies.filter(obj => {
  return obj.imdbID === imdb.imdbID
});

console.log(notresult);

好吧,如果我理解正确的话,它是这样的:

let data = response.data;
let newMovieId = data.imdbID;
ratedMovies.forEach((movie) => {
    if(movie.imdbID === newMovieId) {
        data.userRating = movie.userRating;
    }
});

setModalDetails(data)

上面的代码进入 axios 成功回调

暂无
暂无

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

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