Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery reverse array

Tags:

arrays

jquery

I have the following snippet which returns some youtube id's. Now I want to reverse the output (because now it is the last first)

if (options.slideshow) {
var links = [];
var $lis = holder.parents('#yt_holder').find('li');
var $as = $lis.children('a');
for(var count = $lis.length-1, i = count; i >= 0; i--){
    links.push(youtubeid($as[i].href));
    }
 slideshow = '&playlist=' + links + '';
 alert(slideshow);
}

I tried .reverse() but some items seems to be missing then

links.reverse().push(youtubeid($as[i].href));

Any help will be appreciated. Ceasar

like image 773
ceasar Avatar asked Jan 02 '12 15:01

ceasar


People also ask

How do I reverse an array in ES6?

ES6 - Array Method reverse()reverse() method reverses the element of an array. The first array element becomes the last and the last becomes the first.


1 Answers

You should reverse the list after you've accumulated it:

for ( ... ) {
    ...
}
links = links.reverse();

but it would be better to just put the elements into the array in the right order in the first place.

like image 192
Alnitak Avatar answered Oct 08 '22 04:10

Alnitak