Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!