本文介绍了如何简化else-if语句(其中文本字符串求值与要附加到数组的名称相同)? (JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一系列else-if语句,在对它们进行评估后,会将数据追加到与评估语句中的硬编码字符串同名的数组。我知道必须有一种相当简单的方法来提高效率,但是我想不出一个不会使事情复杂化的解决方案。

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.

代码:

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);
    });
}


推荐答案

为什么不使用动态方法,并忽略与 this 无关的局部变量。

Why not use a dynamic approach, and omit local variables which have nothing to do with this.

categoryObject.data.yAxis.forEach(element => {
    // create array, if not exist
    this[categoryObject.category] = this[categoryObject.category] || [];
    // push value
    this[categoryObject.category].push(element);
});

这篇关于如何简化else-if语句(其中文本字符串求值与要附加到数组的名称相同)? (JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:41