R
Reactiflux

✅ – venus – 09-58 Aug 22

✅ – venus – 09-58 Aug 22

Vvenus8/22/2022
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' },
];
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' ].
SS3BAS8/22/2022
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?
Vvenus8/22/2022
It should be always next to each other.
SS3BAS8/22/2022
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 irrelevant
Vvenus8/22/2022
Sometimes it can be like 👇 , so result would be just one item [{ id: 'two' }]
const selectedState = [
{ id: 'zero' },
{ id: 'one' },
];
const selectedState = [
{ id: 'zero' },
{ id: 'one' },
];
SS3BAS8/22/2022
Yeah, in that case you look at what index findIndex returns
Vvenus8/22/2022
Yeah I was thinking about something similar. This is what I wrote yest.
Get the first and last item in selectedState, find them in initialState & get -1 item from first and +1 from the last
SS3BAS8/22/2022
Yeah, seems like the straight forwarded solution to me
Vvenus8/22/2022
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.
SS3BAS8/22/2022
meowthumbsup
UUUnknown User8/22/2022
2 Messages Not Public
Sign In & Join Server To View
Vvenus8/22/2022
import compact from "lodash/compact";

export const getNextAndPrevious = <P extends { id: any }>(
initialArr: Array<P>,
selectedArr: Array<P>
) => {
const firstItem = selectedArr[0];
const indexOfFirst = initialArr.findIndex((i) => i.id === firstItem?.id);

if (indexOfFirst === -1) return [];

const previous = initialArr[indexOfFirst - 1];
const next = initialArr[indexOfFirst + selectedArr.length];

return compact([previous, next]); // Removes item if it's `undefined`
};
import compact from "lodash/compact";

export const getNextAndPrevious = <P extends { id: any }>(
initialArr: Array<P>,
selectedArr: Array<P>
) => {
const firstItem = selectedArr[0];
const indexOfFirst = initialArr.findIndex((i) => i.id === firstItem?.id);

if (indexOfFirst === -1) return [];

const previous = initialArr[indexOfFirst - 1];
const next = initialArr[indexOfFirst + selectedArr.length];

return compact([previous, next]); // Removes item if it's `undefined`
};
@Timotius What do you think about this?
UUUnknown User8/22/2022
Message Not Public
Sign In & Join Server To View
Vvenus8/22/2022
About mine
SS3BAS8/22/2022
Nice to use the offset of the selectedArr
Vvenus8/22/2022
Sorry, forgot to edit all variables in the function. I was renaming it here on Discord, to make it more clear
UUUnknown User8/22/2022
Message Not Public
Sign In & Join Server To View
Vvenus8/22/2022
I did edit this const firstItem = selectedArr[0]; do you have it same?
UUUnknown User8/22/2022
2 Messages Not Public
Sign In & Join Server To View
Vvenus8/22/2022
Oh, there might be a problem. I realized. Cuz it won't be in order as I thought
const initialState = [
{ id: 'zero' },
{ id: 'one' }, // Previous item
{ id: 'two' }, // Existing array
{ id: 'three' }, // Existing array
{ id: 'four' }, // Existing array
{ id: 'five' }, // Next item
{ id: 'six' },
];

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

const selectedState = [
{ id: 'three' },
{ id: 'four' },
{ id: 'two' },
];
SS3BAS8/22/2022
You'll need to find the lowest index then Or highest eg, findIndex for every element
const indices = selectedState.map(elA => initialState.findIndex(elB => elA.id === elB.id))
const minIndive = Math.min(...indices)
const indices = selectedState.map(elA => initialState.findIndex(elB => elA.id === elB.id))
const minIndive = Math.min(...indices)
UUUnknown User8/22/2022
2 Messages Not Public
Sign In & Join Server To View
Vvenus8/22/2022
How is this meant? Oh nvm, now I can understand
SS3BAS8/22/2022
meowthumbsup
UUUnknown User8/23/2022
2 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

✅ – venus – 09-58 Aug 22

Join Server