<tfoot id='mrYbK'></tfoot>

    <small id='mrYbK'></small><noframes id='mrYbK'>

        • <bdo id='mrYbK'></bdo><ul id='mrYbK'></ul>
      1. <legend id='mrYbK'><style id='mrYbK'><dir id='mrYbK'><q id='mrYbK'></q></dir></style></legend>
        <i id='mrYbK'><tr id='mrYbK'><dt id='mrYbK'><q id='mrYbK'><span id='mrYbK'><b id='mrYbK'><form id='mrYbK'><ins id='mrYbK'></ins><ul id='mrYbK'></ul><sub id='mrYbK'></sub></form><legend id='mrYbK'></legend><bdo id='mrYbK'><pre id='mrYbK'><center id='mrYbK'></center></pre></bdo></b><th id='mrYbK'></th></span></q></dt></tr></i><div id='mrYbK'><tfoot id='mrYbK'></tfoot><dl id='mrYbK'><fieldset id='mrYbK'></fieldset></dl></div>
      2. 如何在 JavaScript 中遍历数组并删除元素

        How to iterate over an array and remove elements in JavaScript(如何在 JavaScript 中遍历数组并删除元素)
          <bdo id='PaoiQ'></bdo><ul id='PaoiQ'></ul>

          • <small id='PaoiQ'></small><noframes id='PaoiQ'>

              <tbody id='PaoiQ'></tbody>
          • <legend id='PaoiQ'><style id='PaoiQ'><dir id='PaoiQ'><q id='PaoiQ'></q></dir></style></legend>
            <i id='PaoiQ'><tr id='PaoiQ'><dt id='PaoiQ'><q id='PaoiQ'><span id='PaoiQ'><b id='PaoiQ'><form id='PaoiQ'><ins id='PaoiQ'></ins><ul id='PaoiQ'></ul><sub id='PaoiQ'></sub></form><legend id='PaoiQ'></legend><bdo id='PaoiQ'><pre id='PaoiQ'><center id='PaoiQ'></center></pre></bdo></b><th id='PaoiQ'></th></span></q></dt></tr></i><div id='PaoiQ'><tfoot id='PaoiQ'></tfoot><dl id='PaoiQ'><fieldset id='PaoiQ'></fieldset></dl></div>

                • <tfoot id='PaoiQ'></tfoot>

                  本文介绍了如何在 JavaScript 中遍历数组并删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个元素数组,需要从中删除某些元素.问题是 JavaScript 似乎没有 for each 循环,如果我使用 for 循环,我会遇到问题,它基本上试图检查数组边界之外的元素,或者由于索引更改而丢失数组中的元素.让我告诉你我的意思:

                  I have an array of elements and need to remove certain ones from it. The problem is that JavaScript doesn't seem to have a for each loop and if I use a for loop I run into problems with it basically trying to check elements beyond the bounds of the array, or missing elements in the array because the indexes change. Let me show you what I mean:

                  var elements = [1, 5, 5, 3, 5, 2, 4];
                  for(var i = 0; i < elements.length; i++){
                      if(elements[i] == 5){
                          elements.splice(i, 1);
                      }
                  }
                  

                  问题是当元素[1]被移除时,元素[2]变成了元素[1].所以第一个问题是某些元素从未被检查过.另一个问题是 .length 发生变化,如果我对边界进行硬编码,那么我可能会尝试检查超出数组边界的元素.那么,完成这个极其简单的事情的最佳方法是什么?

                  The problem is that when elements[1] is removed, elements[2] becomes elements[1]. So first problem is that some elements are never examined. The other problem is that .length changes and if I hard code the bounds, then I might be trying to examine elements beyond the bounds of the array. So what's the best way to do this incredibly simple thing?

                  推荐答案

                  从头开始!

                  var elements = [1, 5, 5, 3, 5, 2, 4];
                  for(var i = elements.length -1; i >= 0 ; i--){
                      if(elements[i] == 5){
                          elements.splice(i, 1);
                      }
                  }
                  

                  这篇关于如何在 JavaScript 中遍历数组并删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                  <tfoot id='oAlF3'></tfoot>
                  • <i id='oAlF3'><tr id='oAlF3'><dt id='oAlF3'><q id='oAlF3'><span id='oAlF3'><b id='oAlF3'><form id='oAlF3'><ins id='oAlF3'></ins><ul id='oAlF3'></ul><sub id='oAlF3'></sub></form><legend id='oAlF3'></legend><bdo id='oAlF3'><pre id='oAlF3'><center id='oAlF3'></center></pre></bdo></b><th id='oAlF3'></th></span></q></dt></tr></i><div id='oAlF3'><tfoot id='oAlF3'></tfoot><dl id='oAlF3'><fieldset id='oAlF3'></fieldset></dl></div>

                    <small id='oAlF3'></small><noframes id='oAlF3'>

                      <tbody id='oAlF3'></tbody>
                      <legend id='oAlF3'><style id='oAlF3'><dir id='oAlF3'><q id='oAlF3'></q></dir></style></legend>
                      • <bdo id='oAlF3'></bdo><ul id='oAlF3'></ul>