如何缩短这个if语句?

时间:2017-05-17 16:31:57

标签: javascript if-statement

我有if(this.exercisesDoneArray[ind][i].done && this.exercisesDoneArray[ind][1].done这样:

&& this.exercisesDoneArray[ind][i].exercise === this.slides.clickedSlide.nextElementSibling.nextElementSibling.id || this.exercisesDoneArray[ind][1].exercise === this.slides.clickedSlide.nextElementSibling.nextElementSibling.id){ select to_timestamp(20130216131736::text, 'YYYYMMDDHH24MISS')

但我想知道如何缩短这个......

2 个答案:

答案 0 :(得分:4)

如何使用一些变量?

var ex1 = this.exercisesDoneArray[ind][i],
    ex2 = this.exercisesDoneArray[ind][1],
    nextSlide = this.slides.clickedSlide.nextElementSibling.nextElementSibling.id;

if(ex1.done && ex2.done && ex1.exercise === nextSlide || ex2.exercise === nextSlide){

答案 1 :(得分:0)

使用少量变量,然后将其放在全局之外,如果你检查你的主要练习是否已经完成另外一个,因为它总是要完成,因此如果使用和

,则不需要总是在另一个内部检查
var currentExcercise = this.exercisesDoneArray[ind][i];
var baseExcercise = this.exercisesDoneArray[ind][1];
var slideId = this.slides.clickedSlide.nextElementSibling.nextElementSibling.id;

if (!baseExcercise.done) {
    return false;
}
if(currentExcercise.done && currentExcercise.exercise === slideId  || baseExcercise .exercise === slideId ){