简体   繁体   English

Javascript:负循环的用例是什么?

[英]Javascript: What is the use case for negative loops?

I normally use for loops which count positively.我通常使用正数的 for 循环。

var i;
for (i = 0; i < cars.length; i++) {
  text += cars[i] + "<br>";
}

I am curious to know in what use case would counting backwards be used?我很想知道在什么用例中会使用倒数?

Here a example:这里有一个例子:

When you retrieve data from a database for example blogposts, the data will be most of the time an array with objects in it and render them via a loop on your website the last data that was inserted in the database will be display at the top.当您从数据库中检索数据(例如博客文章)时,大多数情况下,数据将是一个包含对象的数组,并通过您网站上的循环呈现它们,最后插入数据库的数据将显示在顶部。 Maybe you want to show the oldest blogpost so you could just change your loop to count backwards.也许您想显示最旧的博文,这样您就可以将循环更改为倒数。

I hope you get the point what I mean, that's just a example我希望你明白我的意思,这只是一个例子

Say you have an array of 10 numbers.假设您有一个包含 10 个数字的数组。

const myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Your task is to delete the ones that are divisible by 2 and convert to a string with an a the others (for example 1 will be 1a ).您的任务是删除可被 2 整除的那些并转换为带有a the others 的字符串(例如1将是1a )。 (For the sake of the argument let´s say no map or filter functions exist). (为了论证,假设不存在mapfilter功能)。

If you parse the array from the start for (i = 0; i < myArray.length; i++) , everytime you delete an item with for example splice the array length changes and no all items will be correctly parsed.如果从一开始就解析数组for (i = 0; i < myArray.length; i++) ,则每次使用例如splice删除一个项目时,数组长度都会发生变化,并且不会正确解析所有项目。

BUT if you starts from the end this will be not a problem.但是,如果您从头开始,这将不是问题。

If you were attempting to loop over items in any array-like object (using the .length of the array as your loop boundary) and delete them, you'd find that if you counted incrementally, the loop would not run the correct amount of times because the length would change each time you deleted an item:如果您尝试遍历任何类似 object 的数组中的项目(使用数组的.length作为循环边界)并删除它们,您会发现如果您递增计数,则循环不会运行正确数量的次,因为每次删除项目时length都会改变:

 // Get all the divs into a collection let divs = document.getElementsByTagName("div"); // Set up a loop that will go as many times as there are items in the collection for(var i = 0; i < divs.length; i++){ divs[i].remove(); // Remove the div being iterated from the page }
 <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div>

But, if you remove the elements at the end of the collection and work backwards, the length is adjustment doesn't get out of sync with the contents of the collection:但是,如果您删除集合末尾的元素并向后工作,则length调整不会与集合的内容不同步:

 // Get all the divs into a collection let divs = document.getElementsByTagName("div"); // Set up a loop that will go as many times as there are items in the collection // But count backwards to keep the length from causing issues. for(var i = divs.length - 1; i > -1; i--){ divs[i].remove(); // Remove the div being iterated from the page }
 <div>One</div> <div>Two</div> <div>Three</div> <div>Four</div> <div>Five</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM