First, apologies,this should be simple but I’ve had too much coffee and cannot wrap my tired brain around this (at least not without making it way more complicated than I know it should be).
Lets say I have a simple Javascript array with a number of items in it:
var items = ["Hello", "This", "is", "an", "array", "with",
"a", "number", "of", "items", "in", "it"];
For whatever reason I’m suddenly interested in the 2nd value:
items[1]
//-> "This"
But I also want to get the previous value, and the next two values…
//-> "Hello", "This", "is", "an"
To put it this way:
function getBatch(items, start) {
// What goes here so it would return the results below?
}
getBatch(items, 0);
//-> ["it", "Hello", "This", "is"]
getBatch(items, 4);
//-> ["an", "array", "with", "a"]
getBatch(items, (items.length-1));
//-> ["in" "it", "Hello", "This"]
What is the code for the function getBatch (above) in order to return those result-sets?
Please, no answers dependent on JQuery 🙂
Edited: (removed original version, as it was more crap than this)
Edit 2.1 (bugfixed)
Edit 2.2 (move start to actual start and eliminate one wrap case (final)
Ok, crying boy cared for by his mother. Now, let’s do this correct.