Greetings! I have a JSON data that looks like this:
{
"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
}
}
}
Additionally, I have some TypeScript if statements as shown below,
....
Object.values(data.details.data1).map(obj => {
switch (obj) {
case 'January':
xAxisTranslatedArray.push(this.JAN);
break;
case 'February':
xAxisTranslatedArray.push(this.FEB);
break;
case 'March':
xAxisTranslatedArray.push(this.MAR);
break;
case 'April':
xAxisTranslatedArray.push(this.APR);
break;
case 'May':
xAxisTranslatedArray.push(this.MAY);
break;
case 'June':
xAxisTranslatedArray.push(this.JUN);
break;
case 'July':
xAxisTranslatedArray.push(this.JUL);
break;
case 'August':
xAxisTranslatedArray.push(this.AUG);
break;
case 'September':
xAxisTranslatedArray.push(this.SEP);
break;
case 'October':
xAxisTranslatedArray.push(this.OCT);
break;
case 'November':
xAxisTranslatedArray.push(this.NOV);
break;
case 'December':
xAxisTranslatedArray.push(this.DEC);
break;
}
});
I am making use of lodash, Highcharts, and i18n translations in my project. Each this.MONTH
represents an i18n key. Since direct translation is not possible, I am storing these values in an array to pass them to the X-axis of the Highchart. The current if-else structure seems lengthy and repetitive. Is there a more efficient way to handle this? Thank you for your assistance.