JakubHJ
Reactiflux4y ago
38 replies
JakubH

✅ – venus – 09-58 Aug 22

Hello, I'm trying to think of best solution to get previous and next item of
selectedState
when we fit it inside
initialState
. Length of
selectedState
array could be up to the length of
initialState
. Is there any function that would do it for me, or do I have to write it by myself?

Example:
const initialState = [
  { id: 'zero' },
  { id: 'one' },
  { id: 'two' }, // Previous item
  { id: 'three' }, // Existing array
  { id: 'four' }, // Existing array
  { id: 'five' }, // Next item
  { id: 'six' },
];

const selectedState = [
  { id: 'three' },
  { id: 'four' },
];

So in this case it would be:
[{ id: 'two' }, { id: ' five' ]
.
Was this page helpful?