簡體   English   中英

如何使用模數反轉循環數組項

[英]How to reverse looping array items with 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]

我提前感謝您的幫助!

我想你可以通過這樣做來實現:

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

在哪里:

  • 索引:是您要回顧的位置

  • 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

我認為下面的代碼片段可能有助於反向循環遍歷數組,而無需檢查邊界。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM