Reputation: 13207
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?
Upvotes: 6
Views: 1346
Reputation: 54619
My approach:
function overslice(array, startindex, count, direction) {
var retarray = [];
var increment = (direction === 'backward') ? -1 : 1;
for(var c=0, i = startindex; c<count; i+=increment, c++){
retarray.push(array[(i + array.length)%array.length]);
}
return retarray;
}
UPDATE
Another version using the count
variable to dictate the direction using positive/negative values and with a fix to allow use of count
larger than the array length:
function overslice(array, startindex, count) {
var retarray = [];
var increment = (count >= 0) ? 1 : -1;
count = Math.abs(count);
for(var i = startindex, c = 0;c<count;i+=increment, c++){
if(i<0) i= array.length-1;
retarray.push(array[i%array.length]);
}
return retarray;
}
Upvotes: 1
Reputation: 4144
Hi try this code .
function myslice( ar , start , count , dir ) {
dir = dir || 'F';
var out = [] ,
times = Math.ceil( ( start + count )/ ar.length ) + 1;
while( times-- ) {
[].push.apply( out, ar );
}
if ( dir == 'B' ) {
start = ar.length - start - 1;
out = out.reverse() ;
}
return out.slice( start , start+count );
}
myslice ( [0,1,2,3,4,5,6,7,8,9] , 7 , 5 , 'B' );
Upvotes: 0
Reputation: 4183
To filter invalid positions, I add some validations, without which the code can be even shorter.
var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var countToExtract = 10
var whereToBegin = -1
var direction = "backword"
function overslice(array, startIndex, count, direction) {
var retArray = [];
var step = direction == "backword" ? -1 : 1;
var startPos = (startIndex + array.length) % array.length;
var endPos = (startPos + (count % array.length) * step + array.length) % array.length;
for (var i = 0; i < count; i++) {
var pos = (startPos + (i * step) % array.length + array.length) % array.length;
retArray.push(array[pos]);
}
return retArray;
}
var lampa = overslice(arr, whereToBegin, countToExtract, direction)
alert(lampa)
with code above, you can:
begin from a minus position, which will count back from the other end.
count can be longer than array length, which will return you numbers repeatedly.
Upvotes: 0
Reputation: 94101
How about a function that returns the forward and back arrays? That way you don't need a switch. Something like this:
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); //=> [7, 8, 9, 0, 1] [3, 4, 5, 6, 7]
Upvotes: 1
Reputation: 2783
Just create copy of the array and append the original array until it is long enough:
var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var start = 7
var count = 5
var tmparr = arr
while (tmparr.length < start + count) {
tmparr = tmparr.concat(arr);
}
var result = tmparr.slice(start, start + count);
alert(result);
Upvotes: 0
Reputation: 12683
Its not much, but looks fine JS Bin:
function overSlice(arr, start, count, dir){
if(dir==='backward'){
arr = arr.reverse();
start = arr.length-start-1;
}
var lastIndex = start+count;
return arr.slice(start, lastIndex>arr.length?arr.length: lastIndex)
.concat(arr.slice(0, lastIndex>arr.length?lastIndex-arr.length: 0));
}
var arr = [0,1,2,3,4,5,6,7,8,9];
alert(overSlice(arr,7,5,'backward'));
Upvotes: 0