mardi 28 mars 2017

Is there any better way to handle this function with multiple if statements doing almost the same?

I have this 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 are arrays of numbers. resultsToAdd is an object with variables xx,yy,zz that can be something different than NaN. Is there anyway to make this code more beautiful? These if statements are doing almost the same.

Aucun commentaire:

Enregistrer un commentaire