How Do I Select The Last 5 Elements in Diffrent Array Length While Map With Css,Scss Or TypeScript?

I have this code. This data array length is changing according to data. I want to select last 5 array element with mod.

I tried with css but i cant find a solution. Because number of array element is changing like 23,24,25,21. I need the remainder according to mod 5.


<div className={styles.container}>
      {data?.cast.map((movie) => {
        const mod = data.cast.length%5     
        return (
          <div
            key={movie.id}
            className={styles.container_card}
            style={{ "--n": mod } as React.CSSProperties}
          >
            <MovieCard
              credit={movie}
              categoryType={keys}
              genreId={movie.genre_ids[0]}
            />
          </div>
        );
      })}
    </div>
I am using Scss. I am trying to write a css like example below.

$n:(MOD 5);

        @for $i from 1 through $n {
            &:hover:nth-last-child(#{$i}){
                transform: translateY(-200px);
            }
        }