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

190
Views
I don't want to add duplicate data into my redux store array

I want to add unique data into my store array, but seems to stuck while adding the data and duplicate data is getting pushed into array. I am using typescript.

So my slice function looks like this.

const initialState = {
  dropDownData:{
  programs: [],
  years: []
 }
}
const slice = createSlice({
  name:"filterSlice",
  initialState: initialState,
  reducers:{
     updateProgram:(state, action) => {
 state.dropDownData.programs = [...state.dropDownData.programs, action.payload];
   }
 }
});

I am calling this reducer function in this way: dispatch(updateProgram(program))

So the program i am getting is in the form of array. But I don't need duplicate data from the program array.

So how to update my reducer function.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Instead of this:

state.dropDownData.programs = [...state.dropDownData.programs, action.payload];

Use this:

state.dropDownData.programs = [...(new Set([...state.dropDownData.programs, action.payload]))];

Approach:

  • The javascript Set allows one to retain only unique elements from the parameter.
  • Thus, when a new Set is created with elements from existing two arrays, the Set will keep only the unique elements.
  • Next, we need to use the ... spread-operator to transform the Set back into an array.
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!