简体   繁体   English

如何缩短这个if语句?

[英]How to shorten this if statement?

I have an if statement like this: 我有一个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){ 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){

But I want wondering how can I shorten this.. 但我想知道如何缩短此时间。

How about using some variables? 如何使用一些变量?

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){

Use few variables and then put outside the global if another if where you check if your main excercise has been done, because it has always to be done so no need to check always inside the other if with an and 使用几个变量,然后在全局变量之外放置其他变量(如果您在其中检查您的主要锻炼是否已完成),因为必须始终这样做,因此如果使用and和,则不必总是在另一个变量中进行内部检查

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 ){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM