Iterate an array as a pair (current, next) in JavaScript

前端 未结 13 970
情话喂你
情话喂你 2020-12-09 09:06

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 th

13条回答
  •  眼角桃花
    2020-12-09 09:50

    Simply use forEach with all its parameters for this:

    yourArray.forEach((current, idx, self) => {
      if (let next = self[idx + 1]) {
        //your code here
      }
    })
    

提交回复
热议问题