如何反转模数

时间:2015-09-18 20:12:26

标签: javascript arrays reactjs refactoring modulus

我正在编写一个反应组件,当单击前向 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)]
     })
  }
}

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

indexPos: [Math.abs((vals[0] - 1) % arryLength), Math.abs((vals[1] - 1) % arryLength), Math.abs((vals[2] - 1) % arryLength)]

保持每个值都是正数,但它给了我一个不同的结果,它只能在indexPos的一个值达到0后循环通过2个项目。

使用Math.abs()时会发生这种情况:

indexPos: [0, 1, 2] --> [1, 0, 1] --> [0, 1, 0] --> [1, 0 , 1] ... etc

这就是我希望handleClickRight循环的方式:

indexPos: [0, 1, 2] --> [4, 0, 1] --> [3, 4, 0] --> [2, 3, 4] --> [1, 2, 3] --> [0, 1, 2]

我提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

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

   targetIndex = (arryLength + (index- x)% arryLength ) % arryLength 

哪里:

  • 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
    

答案 1 :(得分:0)

我认为下面的代码片段可能有助于在不检查边界的情况下反方向遍历数组。

int len = arr.length;
int j = len - 1;
j = ( j - 1 + len ) % len