最近写了一段根据滚轮值修改路由的代码。
十分臃肿,代码全是重复的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 ..............................
十分臃肿,代码全是重复的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 ..............................
var menu = scrollArr[i].split(':')[0];
var nextScroleNum =parseInt(scrollArr[i].split(':')[1]);
if(scrollValue<nextScroleNum){
if(this.props.match.params.anchor != menu ){
this.props.history.push('/accountManage/' +menu);
break;
}
}
}
用for来搞似乎也不对。小弟初学比较菜,就大神指点一下
{
var menu = scrollArr[i].split(':')[1];
var nextScroleNum = scrollArr[i].split(':')[0];
if (scrollValue < menu) {
if (this.props.match.params.anchor != nextScroleNum) {
this.props.history.push('/accountManage/' + nextScroleNum); // 0
continue;
}
}
}
改造如下:
for(var i =0;i<scrollArr.length;i++){
var menu = scrollArr[i].split(':')[0];
var nextScroleNum =parseInt(scrollArr[i].split(':')[1]);
if(scrollValue<nextScroleNum){
this.goHash(menu).bind(this);
}
}
当if条件满足时,直接去执行另一个函数。这样就不会继续循环下去了并不知道原理。。
但是这个地址只是一个思路,你也可以在网上查找foreach的其他写法