I want to get array slice w/o one element that coming as argument, but slice should be rounded. See example for better understanding.
type Positions = "top" | "right" | "bottom" | "left"
const fn = (place : Positions) => {
const POSITIONS = ["top", "right", "bottom", "left"]
return getSlice(positions, place)
}
fn("top") // -> ["right","bottom", "left"]
fn("right") // -> ["bottom", "left", "top"]
fn("bottom") // -> ["left", "top", "right"]
fn("right") // -> ["bottom", "left", "top"]
Should works with any array length
>Solution :
You can grab the index from where you want to slice the array.
Then create two arrays one to right and one to left using slice and then concat them.
const fn = (place) => {
const positions = ["top", "right", "bottom", "left"];
const index = positions.indexOf(place);
return positions.slice(index + 1).concat(positions.slice(0, index));
};
console.log(fn("top")); // ["right","bottom", "left"]
console.log(fn("right")); // ["bottom", "left", "top"]
console.log(fn("bottom")); // ["left", "top", "right"]
console.log(fn("right")); // ["bottom", "left", "top"]