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

104
Visualizações
How will I retrieve entries one by one from array and pass it on API?

In the current code, I am taking data in cities and then concate with search. Search has the data in this format (Delhi,Dehradun,Kotdwara,Pune) and when I am passing it through API it is picking the first element (Delhi) and showing only its data. What I want is to pick all the cities one by one and pass them through API one by one. API will run multiple times.

    const [city, setCity] = useState(null);
    const [search, setSearch] = useState("");
    const [istru, setIstru] = useState(false);

    const cities = ["Delhi","Dehradun","Kotdwara","Pune"];
    if(istru==false)
    {
        
        setSearch(search.concat(cities))
        setIstru(true);
    }


    const getCity = async (search) => {
        const url = `https://api.openweathermap.org/data/2.5/weather?q=${search}&units=metric&appid=7938d9005e68d8b258a109c716436c91`;
        const response = await fetch(url);
        const resJson = await response.json();
        const city = resJson.main;
        return city;
      };
    
      const getAllCities = async() => {
        const promises = cities.map(city => getCity(city))
        const cities = await Promise.all(promises);
        // Do whatever you want with all cities
      }
    
      useEffect(() => {
        getAllCities();
      }, [cities]);


return(       
 {!city ? (
            <p className="errorMsg">Enter City Name</p>
           ) : (
            <div>
               
                <div className="info">
                <h2 className="location">
                <i className="fa-solid fa-street-view"> </i>{search}
                </h2>
                <h1 className="temp">
                {cities.temp}
                </h1>
                <h3 className="tempmin_max">
                   Min : {cities.temp_min} | Max : {cities.temp_max}
                </h3>
                </div>
)```
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to do one by one you can do something like this:

 const getCity = async (search) => {
    const url = `https://api.openweathermap.org/data/2.5/weather?q=${search}&units=metric&appid=7938d9005e68d8b258a109c716436c91`;
    const response = await fetch(url);
    const resJson = await response.json();
    const city = resJson.main;
    return city;
  };

  const getAllCities = async() => {
    const promises = cities.map(city => getCity(city))
    const cities = await Promise.all(promises);
    // Do whatever you want with all cities
  }

  useEffect(() => {
    getAllCities();
  }, [cities]);
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