2

I have a jQuery collection of elements (images).

var images = $('img');

I want to alter the collection so that it starts at a specific index, and the previous items are appended to the end. Sort of like a Rolodex.

I could also convert them to an array if necessary. An example of this with a JavaScript array:

var images = ['image0', 'image1', 'image2', 'image3'];
// rotate through to index 2
// images should == ['image2', 'image3', 'image0', 'image1']
0

5 Answers 5

4

For the Array, you could do this:

images.push.apply(images, images.splice(0, i));

var images = ['image0', 'image1', 'image2', 'image3'];
var i = 2;


images.push.apply(images, images.splice(0, i));


document.body.textContent = images.join(",");

There's a limit to how many items can be passed to .apply(), but it's in the thousands. If that's an issue, then you can push individual items from the .splice() in.

Sign up to request clarification or add additional context in comments.

6 Comments

Well, colour me impressed, admittedly this was only my first thought on the problem: jsfiddle.net/davidThomas/6tr33m78 but: well done! :)
What is push and why is it here?
It will work just for static and sorted array we know. But what you will do if it will be dynamic, nonsorted array? I think its a bad way to resolve this problem.
@Andrew: It's a variadic function used to add items to an Array. By calling it via Function.prototype.apply, we can pass an Array of items to add to images, and they will be passed as the individual variadic parameters. The .splice() removes the items and returns them in an Array, so that's what's passed to .apply() and therefore to .push().
@JerryCauser: Not sure what you mean. Sorting should be irrelevant for what the OP wants to do.
|
3

If it was an array, it's trivial with Array.splice:

> x = ['image0', 'image1', 'image2', 'image3'];
[ 'image0',
  'image1',
  'image2',
  'image3' ]
> x = x.splice(2).concat(x)
[ 'image2',
  'image3',
  'image0',
  'image1' ]
> x = ['image0', 'image1', 'image2', 'image3'];
[ 'image0',
  'image1',
  'image2',
  'image3' ]
> x = x.splice(3).concat(x)
[ 'image3',
  'image0',
  'image1',
  'image2' ]
> 

Splice does in-place chopping out of stuff, what you're left with is the spliced first two elements. We put that into X. The original array is modified in place, so it points to what it was at the original referencing time, so, the remaining n elements. So you concat to those remaining elements the spliced first two.

You can even use negative values and play with it, move forward and backward:

> x = ['image0', 'image1', 'image2', 'image3'];
[ 'image0',
  'image1',
  'image2',
  'image3' ]
> x = x.splice(-1).concat(x)
[ 'image3',
  'image0',
  'image1',
  'image2' ]
> x = x.splice(1).concat(x)
[ 'image0',
  'image1',
  'image2',
  'image3' ]
> x = x.splice(-3).concat(x)
[ 'image1',
  'image2',
  'image3',

1 Comment

Very elegant, I like!
1

If you need smart sort function, then you can put function into sort as argument with your algorithm. For your task it can be like that:

var images = ['image5','image0', 'image1', 'image2', 'image3','image4'];
var magicNum = 2 -1;//your offset with shift = 1
images.sort(function(a,b){
  a = parseInt(a.substr(5))-magicNum,
  b = parseInt(b.substr(5))-magicNum;
  
  if(a*b<=0) return a>0?-1:b>0?1:0;
  else return a-b;
});

document.body.textContent = images.join(", ");

It can be used even on big nonsorted dynamic arrays. Also this method more scalable than method with splice and push.

Comments

0
var arr = [2,3,4,5,6,7];

function rot(arr, ind)
{
    var narr = [];
 var len = arr.length;
 for(var i=0; i<arr.length; i++)
 {
     narr.push((i+ind<len?arr[i+ind]:arr[len-i-1]));
 }
    return narr;
}

rot(arr,2); // returns 4,5,6,7,2,3

Comments

0
var data_all= ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul" , 'Aug' ,'Sep' , 'Oct' , 'Nov' , 'Dec'];
data_all.push(data_all.shift());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.