【问题标题】:How do I prevent this last "broken condition" from being pushed to array如何防止最后一个“损坏条件”被推送到数组
【发布时间】:2019-06-22 18:53:07
【问题描述】:

我正在动态切片一个数组,我可以通过简单地使用arr.pop() 删除最后一个元素来获得我想要的功能,但我想知道为什么我的while 循环会在它破坏我的数组时将它添加到我的数组中有条件的。

slices(num){
    let arr = [this.digits.slice(0, num)]
    let i = 0
    if (this.digits.length < num){
            throw new Error('Slice size is too big.')
    } else {
        while (arr[i].length === num){
            i++
            arr.push(this.digits.slice(i, num + i))
        }
        // arr.pop() - removed for testing
        return arr
    }
}

这是一个例子。假设我们要对这个数组进行切片:

this.digits = [ 3, 1, 0, 0, 1 ]

理想情况下,我们的输出将如下所示:

[3, 1, 0], [1, 0, 0], [0, 0, 1]]

使用我当前的代码并且不使用arr.pop(),我的算法将始终潜入一个长度小于我的条件要求的长度的额外切片迭代(在本例中为num == 3

这将是我的输出:

[[3, 1, 0], [1, 0, 0], [0, 0, 1], [0, 1]]

我知道有很多方法可以做到这一点,但为此,我想保持我的代码的完整性,所以使用我的实现的解决方案会很棒:D

编辑:我明白为什么要添加最后一个元素。由于之前的元素满足条件(它的长度等于 num),它会继续进行下一次迭代,但是我如何在不使用 .pop() 的情况下雄辩地处理它

编辑:谢谢大家的回答!它们看起来都可以工作,但是 Peter B 的实现非常干净,特别是考虑到他只为我更改了几行代码,它就像一个魅力。再次感谢!

【问题讨论】:

    标签: javascript arrays while-loop slice


    【解决方案1】:

    您正在检查while 中的错误条件。最好计算一下您要在while 中添加多少子数组(除了您开始的那个),然后数到那个数字,如下所示:

    var digits = [3, 1, 0, 0, 1];
    
    function slices(num) {
      let arr = [this.digits.slice(0, num)]
      let i = 0
      if (this.digits.length < num) {
        throw new Error('Slice size is too big.')
      } else {
        var sliceCutoff = this.digits.length - num;
        while (i < sliceCutoff) {
          i++
          arr.push(this.digits.slice(i, num + i))
        }
        return arr
      }
    }
    
    console.log(slices(3));

    【讨论】:

      【解决方案2】:

      您需要检查剩余的项目是否足以获得所需长度的数组。这意味着,您需要一个循环来持续检查实际的 ster 索引、所需大小和长度。

      不需要在拼接后检查最后一个数组的方法,因为它会产生可避免的开销。

      function slice(array, n) {
          var result = [], 
              start = 0;
          
          while (start + n <= array.length) {
              result.push(array.slice(start, start++ + n));
          }
          return result;
      }
      
      var array = [3, 1, 0, 0, 1];
      
      console.log(slice(array, 3));

      【讨论】:

        【解决方案3】:

        说明

        我相信您正在寻找类似的东西?如您所见,我还删除了一些冗余代码,即在这种情况下使用while 循环和else 子句。

        我还刚刚宣布digits 作为此演示的参数,我相信您可以主动将其更改为您的应用程序要求而无需太多/任何帮助。

        function slices(digits, num) {
          const arr = [];
        
          if (digits.length < num)
            throw new Error('Slice size is too big.')
        
          for (let i = 0; i != num; i++)
            arr.push(digits.slice(i, num + i));
        
          return arr;
        }
        
        var d = [3, 1, 0, 0, 1]; // Only here for the demo.
        console.log(slices(d, 3));

        【讨论】:

          【解决方案4】:

          你真的很亲密。我认为我在这里提出的解决方案保留了您的总体思路。您遇到的问题是检查 arr[i].length 是否等于 num 意味着这仅检查您添加到数组中的最后一项,而不是下一项。相反,请检查您要添加的项目。

          this.digits = [ 3, 1, 0, 0, 1 ];
          
          function slices(num) {
              let arr = []
              let i = 0
              if (this.digits.length < num) {
                      throw new Error('Slice size is too big.')
              } else {
                  while (this.digits.slice(i, num + i).length === num){
                      arr.push(this.digits.slice(i, num + i))
                      i++
                  }
                  // arr.pop() - removed for testing
                  return arr
              }
          }
          
          console.log(slices(3));

          【讨论】:

            猜你喜欢
            • 2015-04-16
            • 1970-01-01
            • 1970-01-01
            • 2021-11-12
            • 1970-01-01
            • 1970-01-01
            • 2019-05-16
            • 1970-01-01
            • 2014-06-03
            相关资源
            最近更新 更多