如何反向循环遍历 cheerio 对象?

How to loop through cheerio object in reverse?

https://github.com/cheeriojs/cheerio

让我们保存一个我想要反向循环的一长串元素 (1000+)。最有效的方法是什么?谢谢

$('li').each(function(i, elem) {
  fruits[i] = $(this).text();
});

for 循环怎么样?

const li = $('li');

for (let i = li.length - 1; i >= 0; i--) {
  $(li[i]).text();
}

一种方法是使用 $('li').size() - 1 - i 作为索引:

const fruits = [];

$('li').each(function(i, elem) {
  fruits[$('li').size() - 1 - i] = $(this).text();
});

console.log(fruits);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<li>Apple</li>
<li>Banana</li>
<li>Pear</li>