【发布时间】:2017-03-31 04:00:27
【问题描述】:
我能够通过阅读另一个问题来弄清楚如何通过我的数组进行递增。现在我似乎无法弄清楚如何减少,因为我的值被设置回 0。我试图避免循环。
我希望达到的顺序是:
增量 ---- "/", "/about", "/list"
let i = 0;
let stuff =["/", "about","list"];
next() {
this.props.dispatch(increaseCounter())
i = (i+1)%stuff.length;
}
prev() {
this.props.dispatch(decreaseCounter())
i = (i-1)%stuff.length; <------This gets wonky once I reach the end of my array.
}
【问题讨论】:
-
你想要增量值去:
0, 1, 2, 0, 1, 2...和减量2, 1, 0, 2, 1, 0...吗? -
嘿,Fubar,它绑定到另一个组件中。所以我要做的就是递增:0,1,2 然后什么都不返回。递减 2,1,0 什么也不返回
-
@user992731 对于“然后不返回任何内容”,只需使用
if语句。 -
谢谢Bergi,成功了
标签: javascript increment modulus