How to early break reduce() method?

后端 未结 12 1175
忘掉有多难
忘掉有多难 2020-11-28 05:44

How can I break the iteration of reduce() method?

for:

for (var i = Things.length - 1; i >= 0; i--) {
  if(Things[i] <=         


        
12条回答
  •  -上瘾入骨i
    2020-11-28 06:25

    Another simple implementation that I came with solving the same issue:

    function reduce(array, reducer, first) {
      let result = first || array.shift()
    
      while (array.length > 0) {
        result = reducer(result, array.shift())
        if (result && result.reduced) {
          return result.reduced
        }
      }
    
      return result
    }
    

提交回复
热议问题