What about a function that returns front and back arrays? This way you do not need a switch. Something like that:
function overslice(arr, idx, items) { var fwd = arr.filter(function(v,i){ return i >= idx }).slice(0, items), back = arr.filter(function(v,i){ return i <= idx }).slice(-items); while (fwd.length < items) { fwd = fwd.concat(arr).slice(0, items); } while (back.length < items) { back = arr.concat(back).slice(-items); } return { fwd: fwd, back: back }; }
Then you can use it like:
var array = [0,1,2,3,4,5,6,7,8,9] var result = overslice(array, 7, 5); console.log(result.fwd, result.back);
source share