从 Javascript 函数创建点赞数组

Creating an Array of Likes from Javascript Functions

这些是我整个代码的一部分。所以我想做的是创建我喜欢或不喜欢的值的单独数组,并将它们输出到我的 html 文件 onclick 中。我尝试创建一个空数组并推送值,但我的最终数组最终为空。

Script.js

const showRandomMovie = async() => {
  const movieInfo = document.getElementById('movieInfo');
  if (movieInfo.childNodes.length > 0) {
    clearCurrentMovie();
  };
  const movies = await getMovies();
  const randomMovie = getRandomMovie(movies);
  const info = await getMovieInfo(randomMovie);
  displayMovie(info);

};
playBtn.onclick = showRandomMovie;

helper.js

const displayMovie = (movieInfo) => {
    const moviePosterDiv = document.getElementById('moviePoster');
    const movieTextDiv = document.getElementById('movieText');
    const likeBtn = document.getElementById('likeBtn');
    const dislikeBtn = document.getElementById('dislikeBtn');
  
    // Create HTML content containing movie info
    const moviePoster = createMoviePoster(movieInfo.poster_path);
    const titleHeader = createMovieTitle(movieInfo.title);
    const overviewText = createMovieOverview(movieInfo.overview);
    const releaseHeader = createReleaseDate(movieInfo.release_date)
    // Append title, poster, and overview to page
    moviePosterDiv.appendChild(moviePoster);
    movieTextDiv.appendChild(titleHeader);
    movieTextDiv.appendChild(overviewText);
    movieTextDiv.appendChild(releaseHeader)

    showBtns();
    likeBtn.onclick = likeMovie;
    dislikeBtn.onclick = dislikeMovie;
};

const likeMovie = () => {
    
    clearCurrentMovie();
    showRandomMovie();
};

// After disliking a movie, clears the current movie from the screen and gets another random movie
const dislikeMovie = () => {
    clearCurrentMovie();
    showRandomMovie();
};

为喜欢和不喜欢创建数组并将其推送到数组。将其传递给您的方法。

likeBtn.onclick = () => rateMovie('likes', movieInfo);
dislikeBtn.onclick = () => rateMovie('dislikes', movieInfo);

让方法将它添加到数组

const ratings = {
  likes: [],
  dislikes: [],
};

const rateMovie = (type, data) => {
    ratings[type].push(data);
    clearCurrentMovie();
    showRandomMovie();
};