• <bdo id='fKohw'></bdo><ul id='fKohw'></ul>
  • <small id='fKohw'></small><noframes id='fKohw'>

    <tfoot id='fKohw'></tfoot>

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

    <legend id='fKohw'><style id='fKohw'><dir id='fKohw'><q id='fKohw'></q></dir></style></legend>
      1. 在 JavaScript 中将数组作为一对(当前,下一个)迭代

        时间:2024-04-18
        1. <small id='mpqcL'></small><noframes id='mpqcL'>

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

                <legend id='mpqcL'><style id='mpqcL'><dir id='mpqcL'><q id='mpqcL'></q></dir></style></legend>

                1. <tfoot id='mpqcL'></tfoot>

                  本文介绍了在 JavaScript 中将数组作为一对(当前,下一个)迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  In the question Iterate a list as pair (current, next) in Python, the OP is interested in iterating a Python list as a series of current, next pairs. I have the same problem, but I'd like to do it in JavaScript in the cleanest way possible, perhaps using lodash.

                  It is easy to do this with a simple for loop, but it doesn't feel very elegant.

                  for (var i = 0; i < arr.length - 1; i++) {
                    var currentElement = arr[i];
                    var nextElement = arr[i + 1];
                  }
                  

                  Lodash almost can do this:

                  _.forEach(_.zip(arr, _.rest(arr)), function(tuple) {
                    var currentElement = tuple[0];
                    var nextElement = tuple[1];
                  })
                  

                  The subtle problem with this that on the last iteration, nextElement will be undefined.

                  Of course the ideal solution would simply be a pairwise lodash function that only looped as far as necessary.

                  _.pairwise(arr, function(current, next) {
                    // do stuff 
                  });
                  

                  Are there any existing libraries that do this already? Or is there another nice way to do pairwise iteration in JavaScript that I haven't tried?


                  Clarification: If arr = [1, 2, 3, 4], then my pairwise function would iterate as follows: [1, 2], [2, 3], [3, 4], not [1, 2], [3, 4]. This is what the OP was asking about in the original question for Python.

                  解决方案

                  Just make the "ugly" part into a function and then it looks nice:

                  arr = [1, 2, 3, 4];
                  
                  function pairwise(arr, func){
                      for(var i=0; i < arr.length - 1; i++){
                          func(arr[i], arr[i + 1])
                      }
                  }
                  
                  pairwise(arr, function(current, next){
                      console.log(current, next)
                  })
                  

                  You can even slightly modify it to be able to make iterate all i, i+n pairs, not just the next one:

                  function pairwise(arr, func, skips){
                      skips = skips || 1;
                      for(var i=0; i < arr.length - skips; i++){
                          func(arr[i], arr[i + skips])
                      }
                  }
                  
                  pairwise([1, 2, 3, 4, 5, 6, 7], function(current,next){
                      console.log(current, next) // displays (1, 3), (2, 4), (3, 5) , (4, 6), (5, 7)
                  }, 2)
                  

                  这篇关于在 JavaScript 中将数组作为一对(当前,下一个)迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 jQuery.each() util 中跳到下一次迭代? 下一篇:为什么 $.each() 不遍历每个项目?

                  相关文章

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

                  <legend id='QPlg5'><style id='QPlg5'><dir id='QPlg5'><q id='QPlg5'></q></dir></style></legend>
                    <tfoot id='QPlg5'></tfoot>
                    • <bdo id='QPlg5'></bdo><ul id='QPlg5'></ul>

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