javascript - Is there any better way to handle this function with multiple if statements doing almost the same? -
i have function:
private addadditionalresults(resultstoadd: any) { if(!isnan(resultstoadd.xx) && this.x.length > 0) this.x.unshift(resultstoadd.xx); if(!isnan(resultstoadd.yy) && this.y.length > 0) this.y.unshift(resultstoadd.yy); if(!isnan(resultstoadd.zz) && this.z.length > 0) this.z.unshift(resultstoadd.zz); }
x,y,z arrays of numbers. resultstoadd object variables xx,yy,zz can different nan. there anyway make code more beautiful? these if statements doing same.
you can create variables array inside function , iterate on them. affect program response because of loop.
results = array(resultstoadd.xx, resultstoadd.yy, resultstoadd.zz); numbers = array(this.x, this.y, this.z); for(i = 0; < results.length; i++){ if(!isnan(results[i]) && numbers[i] > 0) numbers[i].unshift(results[i]); }
Comments
Post a Comment