jQuery jquery反向数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8702849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:55:17  来源:igfitidea点击:

jquery reverse array

jqueryarrays

提问by ceasar

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)

我有以下代码段,它返回一些 youtube id。现在我想反转输出(因为现在它是最后一个)

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

我试过 .reverse() 但有些项目似乎丢失了

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

Any help will be appreciated. Ceasar

任何帮助将不胜感激。凯撒

回答by Alnitak

You should reverse the list afteryou'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.

但最好首先将元素按正确的顺序放入数组中。

回答by Jason Gennaro

Try adding the videos in the reverse order, so instead of this

尝试以相反的顺序添加视频,而不是这样

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

Do this

做这个

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

回答by Aakash Dhoundiyal

Hi you after reversing the links array ,you have to assign it to other array and hence it will work.

嗨,在反转链接数组后,您必须将其分配给其他数组,因此它会起作用。

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