简体   繁体   中英

Javascript: stuck with comparison function

I am stuck and need help finishing this fetch request. I need a function to check if the movie (a single object) in the request has previously been rated by the user.

The ratings are in the ratedMovies array. If the movie was rated I need the userRating property with it's value to be added to the response. If it has not been rated, I need the userRating value to be 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))
  }

Sample axios response:

{
    "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"
}

Sample rating:

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

You can use .filter function like this:

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);

Ok if I understand it correctly it goes like this:

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

setModalDetails(data)

code above goes inside the axios success callback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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