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

110
Vistas
I want to modify my existing array with new values

I am trying to modify my array every time with new values but not able to achieve that functionality.

So the code for pushing array elements is given like this:

const updateYears =  (state, action) => {
      state.dropDownData.years = [...new Set([action.payload])];      
    }

And my initial state is like this:

const initialState = {  
  dropDownData: {
    programs: [],
    years: [],
  }, 
};

The above reducer function is pushing elements inside "years" array.

locationSummaries.map((locationSummary)=>dispatch(updateYears(locationSummary.year)));

locationSummaries is the array of objects containing year as one of the properties.

So when my year array contains the values [2017, 2018, 2019] and when I am pushing [2018, 2019] inside this, I am still having [2017, 2018, 2019] inside the original year array. Instead what I want is modified year array to [2018, 2019]. I believe it is happening because same values are getting pushed inside the array.

So how should I modify my reducer function to achieve this modification.

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

0

If action.payload is array and if you want to modify the array completely, why don't you just assign new array like this:

    const updateYears = (state, action) => {
        state.dropDownData.years = action.payload;
    }
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