简体   繁体   English

简化if else语句

[英]Simplifying if else statements

Hi I have a json data as below: 嗨我有一个json数据如下:

{
    "details":
        {
            "data1": 
                {
                    "monthToDate":1000,                   
                    "firstLastMonth":"December",
                    "firstLastMonthAmount":5000,
                    "secondLastMonth":"November",
                    "secondLastMonthAmount":12000
                },
            "data2":
                {
                    "monthToDate":4000,                   
                    "firstLastMonth":"December",
                    "firstLastMonthAmount":10000,
                    "secondLastMonth":"November",
                    "secondLastMonthAmount":15000
                },
           "data3":
                {
                    "monthToDate":2000,                   
                    "firstLastMonth":"December",
                    "firstLastMonthAmount":8000,
                    "secondLastMonth":"November",
                    "secondLastMonthAmount":12000
                }
        }   
}

And I have typescript if statements below, 如果下面的陈述,我有打字稿,

....
Object.values(data.details.data1).map(obj => {
                if (obj === 'January') {
                    xAxisTranslatedArray.push(this.JAN);
                } else if (obj === 'February') {
                    xAxisTranslatedArray.push(this.FEB);
                } else if (obj === 'March') {
                    xAxisTranslatedArray.push(this.MAR);
                } else if (obj === 'April') {
                    xAxisTranslatedArray.push(this.APR);
                } else if (obj === 'May') {
                    xAxisTranslatedArray.push(this.MAY);
                } else if (obj === 'June') {
                    xAxisTranslatedArray.push(this.JUN);
                } else if (obj === 'July') {
                    xAxisTranslatedArray.push(this.JUL);
                } else if (obj === 'August') {
                    xAxisTranslatedArray.push(this.AUG);
                } else if (obj === 'September') {
                    xAxisTranslatedArray.push(this.SEP);
                } else if (obj === 'October') {
                    xAxisTranslatedArray.push(this.OCT);
                } else if (obj === 'November') {
                    xAxisTranslatedArray.push(this.NOV);
                } else if (obj === 'December') {
                    xAxisTranslatedArray.push(this.DEC);
                }
            });

Am using lodash, highcharts and i18n translations. 我使用lodash,highcharts和i18n翻译。 So each of my this.MONTH is i18n keys. 所以我的每一个this.MONTH都是i18n键。 I can't directly pass the values since not able to translate them. 我无法直接传递值,因为无法翻译它们。 So I needed to push to an array each values and pass into highchart's X-axis. 所以我需要将每个值推送到一个数组并传递到highchart的X轴。 My question is basically when I see the if else statements it looks too long and kind of repetitive. 我的问题基本上是当我看到if else语句看起来太长而且有点重复时。 Is there any short cut here? 这里有捷径吗? Thanks in advance guys. 先谢谢你们。

Use a separate map object: 使用单独的地图对象:

const months = {
    January: this.JAN,
    February: this.FEB,
    March: this.MAR,
    // etc,
}

Object.values(data.details.data1).forEach(obj => {
    if (months[obj]) {
        xAxisTranslatedArray.push(months[obj]);
    }
});

Alternatively, you can replace the if with a filter : 或者,您可以使用filter替换if

Object.values(data.details.data1)
    .filter(obj => months[obj])
    .forEach(obj => xAxisTranslatedArray.push(months[obj]));

Also, note that I'm using forEach instead of map . 另外,请注意我使用forEach而不是map map is used to modify an array, but you're only iterating over an array. map用于修改数组,但您只是在数组上进行迭代。 forEach is the semantically correct choice there. forEach是那里语义正确的选择。

You have to use bracket notation. 您必须使用bracket表示法。

Also, use substring or slice methods in order to get first three characters. 此外,使用substringslice方法以获得前three字符。

Also, don't forget to check if obj is a string . 另外,不要忘记检查obj是否为string

var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
if(typeof obj === 'string' && months.includes(obj))
    xAxisTranslatedArray.push(this[obj.substring(0,3).toUpperCase()]);

You can use a map definition, like so: 您可以使用地图定义,如下所示:

let mapping = {};
mapping['January'] = this.JAN;
... // rest of definitions.


Object.values(data.details.data1).forEach(obj => {
              if(mapping[obj]){
                  xAxisTranslatedArray.push(mapping[obj]);
              });

Seems like obj 's first three characters converted to uppercase is what you want as a key, try 看起来像obj前三个字符转换为大写是你想要的关键,试试

var monthArray = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
Object.values( data.details.data1 ).forEach(obj => {
   if ( monthArray.indexOf( obj ) != -1 )
   {
      xAxisTranslatedArray.push(this[ obj.substring(0,3).toUpperCase() ]);
   }
});

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

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