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

118
Visualizações
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 Respostas
Responde à pergunta

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 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