Jquery reverse array

I have the following snippet that returns some youtube identifiers. Now I want to change the result (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 elements seem to be missing, then

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

Any help would be appreciated. Ceasar

+6
source share
3 answers

You must cancel the list after you have accumulated it:

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

but it would be better to just put the elements in the array in the correct order first.

+9
source

Try adding the video in the reverse order, so instead

 for(var count = $lis.length-1, i = count; i >= 0; i--){ links.push(youtubeid($as[i].href)); } 

Do it

 for(var i = 0, count = $lis.length; i < count; i++){ links.push(youtubeid($as[i].href)); } 
+2
source

Hi, after accessing the array of links, you have to assign it to another array and therefore it will work.

 var slideshow = []; slideshow = links.reverse(); 
0
source

Source: https://habr.com/ru/post/904920/


All Articles