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

160
Views
Keep 10 item in dimensional array in Redux

i have a state in redux that it has Two dimensions 1.user id and 2.product id

return {
  ...state,
  [action.payload.user.id]: {
    ...state[action.payload.user.id],
    [action.payload.product.id]: {
      data: action.payload.product
    }
  }
}

i want to keep last 10 product for each user in my state

please help me ? any suggestion?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

When ever, you are adding new product to user, you get the last 9 (upto) and add the new one. Use slice(-9) will give last 9 items in array.

Update: as @JakubKotrs pointed out, Updating to maintain as array of products for user.

return {
  ...state,
  [action.payload.user.id]: [
    ...state[action.payload.user.id].slice(-9),
    {
      [action.payload.product.id]: {
        data: action.payload.product,
      },
    },
  ],
};
about 4 years ago · Santiago Gelvez 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!