Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Round get array slice roundly except on array value with same order

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading