Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
map through two arrays React

I am wanting to display information on my app from two mapped arrays.

{filmWorld.Provider &&
        filmWorld.Movies.map((movie, index) => {
          return (
            <div className="movies" key={index}>
              <h3>{movie.Title}</h3>
              <img src={movie.Poster} alt={movie.Title} />
              <h5>{movie.Actors} </h5>
              <h6> Price: ${movie.Price}</h6>
              <hr />
            </div>
          );
        })}

This is currently working fine but I'm wanting to have another price displayed from another cinema ideally in the same h6. All the other information is the same.

the other map would be very similar:

{cinemaWorld.Provider &&
        cinemaWorld.Movies.map((movie, index) => {
          return (
            <div className="movies" key={index}>
              
          <h6> Price: ${movie.Price}</h6>
          
        </div>
      );
    })}

but again i would only need the price from this cinema to be displayed on the same h6 tag. Or would it be more intuitive to take a different approach like merging the variables?

The ideal end result code would return something like this:

 return (
            <div className="movies" key={index}>
              <h3>{movie.Title}</h3>
              <img src={movie.Poster} alt={movie.Title} />
              <h5>{movie.Actors} </h5>
              <h6> FilmWorld Price: ${movie.Price}
                   CinemaWorld Price: ${movie.Price}</h6>
              <hr />
            </div>
          );
        })}

So the page would display the movie name, a picture of the movie poster a list of the actors and a price comparison of the two cinemas showing the movie. I'm storing data from two different apis in these variables.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'd use a memo hook to create the data structure you want

const movies = useMemo(() => {
  if (!filmWorld.Provider) return [];
  return filmWorld.Movies.map((movie, index) => ({
    ...movie,
    cinemaWorldPrice: cinemaWorld.Provider && cinemaWorld.Movies[index]?.Price,
  }));
}, [filmWorld, cinemaWorld]);

This grabs the corresponding index from cinemaWorld.Movies (with a condition on cinemaWorld.Provider).

Now you can just iterate the movies array

{
  movies.map((movie, index) => (
    <div className="movies" key={index}>
      <h3>{movie.Title}</h3>
      <img src={movie.Poster} alt={movie.Title} />
      <h5>{movie.Actors} </h5>
      <h6>
        Price: ${movie.Price}
        CinemaWorld Price: ${movie.cinemaWorldPrice}
      </h6>
      <hr />
    </div>
  ));
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda