如何使用模数反转循环数组项

Yi *_*Ren 1 javascript arrays refactoring modulus reactjs

我正在编写一个反应组件,当单击前进handleClickLeft或后退 handleClickRight按钮时,它会向前或向后循环遍历数组。我通过使用模逻辑来做到这一点。我能够让前进按钮handleClickLeft正常工作,但我不知道如何反向操作handleClickRight。这是我的示例代码:

export default class Rooms extends React.Component {
   constructor(props, context) {
      super(props, context);
      this.state = {indexPos: [0, 1, 2]};
      this.state.itemArry = [{room1: 'this is room1'}, {room2: 'this is room2'}, {room3: 'this is room3'}, {room4: 'this is room4'}];

      this.handleClickLeft = this.handleClickLeft.bind(this);
      this.handleClickRight = this.handleClickRight.bind(this);
   }
   render() {   //Using index to show each item in the itemArry
      let firstItem = this.state.indexPos[0]
      let secondItem = this.state.indexPos[1]
      let thirdItem = this.state.indexPos[2]       
      <div>
         <ul>
           <li>this.state.itemArry[firstItem]</li>
           <li>this.state.itemArry[secondItem]</li>
           <li>this.state.itemArry[thirdItem]</li>
         </ul>
      </div>
   }

   handleClickLeft(){     // This one is working, it loops through the array in order and only shows three items at once. Ex: every time the forward button is clicked, indexPos changes >> [0, 1, 2] --> [1, 2, 3] --> [2, 3, 0]... 
      let vals = this.state.indexPos;
      let arryLength = this.state.itemArry.length;
      this.setState({
         indexPos: [(vals[0] + 1) % arryLength, (vals[1] + 1) % arryLength, (vals[2] + 1) % arryLength]
      });
   }

  handleClickRight(){  //This one is NOT working. It should be going in reverse 
     let vals = this.state.indexPos;
     let arryLength = this.state.itemArry.length;
     this.setState({
        indexPos: [(vals[0] - 1 % arryLength), (vals[1] - 1 % arryLength), (vals[2] - 1 % arryLength)]
     })
  }
}
Run Code Online (Sandbox Code Playgroud)

handleClickRight函数中,当任何一个indexPos值达到0时,都会中断脚本。我明白其背后的原因;这是因为负值。我使用 Math.abs():

indexPos: [Math.abs((vals[0] - 1) % arryLength), Math.abs((vals[1] - 1) % arryLength), Math.abs((vals[2] - 1) % arryLength)]
Run Code Online (Sandbox Code Playgroud)

保持每个值都为正,但它给了我一个不同的结果,在其中一个 indexPos 值达到 0 后,它只循环遍历 2 个项目。

这是使用 Math.abs() 时发生的情况:

indexPos: [0, 1, 2] --> [1, 0, 1] --> [0, 1, 0] --> [1, 0 , 1] ... etc
Run Code Online (Sandbox Code Playgroud)

这就是我想要handleClickRight循环的方式:

indexPos: [0, 1, 2] --> [4, 0, 1] --> [3, 4, 0] --> [2, 3, 4] --> [1, 2, 3] --> [0, 1, 2]
Run Code Online (Sandbox Code Playgroud)

我很感谢提前的帮助!

Pra*_*ran 6

我想你可以通过这样做来实现这一点:

   targetIndex = (arryLength + (index- x)% arryLength ) % arryLength 
Run Code Online (Sandbox Code Playgroud)

在哪里:

  • index:是你要回看的位置

  • x:是您要回顾的项目数

    explanation: 
     in Modulo arithmetic adding arryLength any number of times to an index and doing a mod % arryLength will not change the position of the index within the array
     -(index- x)% arryLength could result in a negative value
     -this value would lie between -arryLength and +arryLength (non inclusive)
     -now adding arryLength to the resulting value and taking the mod again we get a value between 0 and arryLength
    
    Run Code Online (Sandbox Code Playgroud)