vendredi 21 juin 2019

How to simplify else-if statements where text string evaluation has the same name as array getting appended to? (JavaScript)

The following is a series of else-if statements, where once evaluated, appends data to an array with the same name as the hard-coded strings in the evaluation statement. I know there must be a fairly easy way to make this more efficient but I cannot think of a solution that doesn't over complicate things.

Code:

name = [];
name2 = [];
name3 = [];
name4 = [];
name5 = [];
name6 = [];
name7 = [];
name8 = [];
name9 = [];
name10 = [];
name11 = [];
name12 = [];
name13 = [];

if (categoryObject.category === 'name1') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name1.push(element);
});
} else if (categoryObject.category === 'name2') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name2.push(element);
    });
} else if (categoryObject.category === 'name3') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name3.push(element);
    });
} else if (categoryObject.category === 'name4') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name4.push(element);
    });
} else if (categoryObject.category === 'name5') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name5.push(element);
    });
} else if (categoryObject.category === 'name6') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name6.push(element);
    });
} else if (categoryObject.category === 'name7') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name7.push(element);
    });
} else if (categoryObject.category === 'name8') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name8.push(element);
    });
} else if (categoryObject.category === 'name9') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name9.push(element);
    });
} else if (categoryObject.category === 'name10') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name10.push(element);
    });
} else if (categoryObject.category === 'name11') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name11.push(element);
    });
} else if (categoryObject.category === 'name12') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name12.push(element);
    });
} else if (categoryObject.category === 'name13') {
    categoryObject.data.yAxis.forEach(element => {
        // console.log('y: ' + element);
        this.name13.push(element);
    });
}

Aucun commentaire:

Enregistrer un commentaire