Extract x items from array (slice) but continue from beginning if end is reached

Lets say I have: var array = [0,1,2,3,4,5,6,7,8,9] I define: var itemsToExtract = 5 var startindex = 7 var direction = "forward" I want to be able to do: array.someMethod(startindex, itemsToExtract, direction) and get [7,8,9,0,1] I also want it to work backwards, if I set direction to "backward" (slicing from right to left). I wasnt too lazy and tried something already, see here: http://jsfiddle.net/MkNrr/ I am looking for something "tidier" if there is, also is there a name for this method, is it a known problem? Background: I am trying to build a sequential pre image loader (load one image(src) after the other) for use in an image gallery. Maybe even such a libary already exists?

以上就是Extract x items from array (slice) but continue from beginning if end is reached的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏