✅ – 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:
So in this case it would be: [{ id: 'two' }, { id: ' five' ]
.24 Replies
Will the items in
selectedState
always be in order next to each other?
In other words, something with id three and id five will not exist in selectedState
?It should be always next to each other.
I would do
findIndex
on the first element of selectedState
and then -1 to find previous item, then repeat for the last item of selectedState
and +1 the index (take care of the boundaries)
Anything that goes in between seems irrelevantSometimes it can be like 👇 , so result would be just one item
[{ id: 'two' }]
Yeah, in that case you look at what index
findIndex
returnsYeah I was thinking about something similar.
This is what I wrote yest.
Get the first and last item inselectedState
, find them ininitialState
& get -1 item from first and +1 from the last
Yeah, seems like the straight forwarded solution to me
Okay, thank you. I'll keep this thread open to let you know how it goes. Just maybe some little correction of the code at the end.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
@Timotius What do you think about this?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
About mine
Nice to use the offset
of the
selectedArr
Sorry, forgot to edit all variables in the function. I was renaming it here on Discord, to make it more clear
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I did edit this
const firstItem = selectedArr[0];
do you have it same?Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Oh, there might be a problem. I realized.
Cuz it won't be in order as I thought
You'll need to find the lowest index then
Or highest
eg,
findIndex
for every element
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
How is this meant?
Oh nvm, now I can understand
This question has an answer! Thank you for helping 😄
If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1011212650090340412
This thread hasn’t had any activity in 12 hours, so it’s now locked.
Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1011212650090340412