Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
Add new element to an array each time the function is called

this is probably very easy but I'm trying to add the current weather location to an array each time the 'add to favorite' button is clicked. The location changes every time the user is searching for an other one. So each time he's looking for a new location I want the ability to add this to an array so I can after display all fav locations to a specific page. Right now each time the function is called its working but if I do it again with another location it rewrites the first one and the array remains with only 1 element. Anybody to help ?

Thanks

  let arr = [];

  const favCity = () => {
    debugger;
    let newArr = [...arr];
    let favCity = weatherSelector.weatherInfo.location.name;
    newArr.push(favCity);
    arr.push(newArr[0]);
  };




<div className="details">
        <div className="weather_text">
          <div className="city_tempa">
            <p className="city_name" onChange={(e) => favCity("city", e.target.value)}>
              {weatherSelector.weatherInfo.location.name}
            </p>
            <p className="city_name">, {weatherSelector.weatherInfo.location.country}</p>
            <br />
            <p className="city_temp">{Math.round(weatherSelector.weatherInfo.current.temp_c * 10) / 10}c°</p>
          </div>
          <div className="weather_icon">
            <img className="city_icon" src={weatherSelector.weatherInfo.current.condition.icon} />
            <p className="city_text">{weatherSelector.weatherInfo.current.condition.text}</p>
          </div>
          <div className="fav">
            <button className="fav_btn" onClick={favCity}>
              Add to Favorite
            </button>
          </div>
        </div>

Thanks everybody :)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The array you need is the newArr, which contains the new locations. So you just need to assign this newArr to arr.

      let arr = [];
    
      const favCity = () => {
        let newArr = [...arr];
        let favCity = weatherSelector.weatherInfo.location.name;
        newArr.push(favCity);
        arr = newArr
      };
about 4 years ago · Juan Pablo Isaza Denunciar

0

Is selecting a new location reloading the page? Because your code doesnt seem to have any problems(as far as we ignore creating a new array for no reason, you can just do arr.push(favCity).

Because that would mean arr = [] will be inicialized again... if you can post minimal reproducible example (like codepen.io), that would surely clarify a few things

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda