求大量范围型if else代码的优化思路
最近写了一段根据滚轮值修改路由的代码。
十分臃肿,代码全是重复的if else 只有参数的下标一直在递增,请问有什么办法缩减优化吗??
代码片段如下:
if (scrollValue < scrollArr[0].split(':')[1]) {
if (this.props.match.params.anchor != scrollArr[0].split(':')[0]) {
this.props.history.push('/accountManage/' + scrollArr[0].split(':')[0]); // 0
}
} else if (scrollValue < scrollArr[1].split(':')[1]) {
if (this.props.match.params.anchor != scrollArr[1].split(':')[0]) {
this.props.history.push('/accountManage/' + scrollArr[1].split(':')[0]); //1
}
} else if (scrollValue < scrollArr[2].split(':')[1]) {
if (this.props.match.params.anchor != scrollArr[2].split(':')[0]) {
this.props.history.push('/accountManage/' + scrollArr[2].split(':')[0]); //2
}
} else if (scrollValue < scrollArr[3].split(':')[1]) {
if (this.props.match.params.anchor != scrollArr[3].split(':')[0]) {
this.props.history.push('/accountManage/'+ scrollArr[3].split(':')[0]); // 3
}
} else if (scrollValue < scrollArr[4].split(':')[1]) {
if (this.props.match.params.anchor != scrollArr[4].split(':')[0]) {
this.props.history.push('/accountManage/'+ scrollArr[4].split(':')[0]);// 4
}
}else ..............................