Currently, I am constructing an object that will contain a key and its corresponding values:
const colorRedCodes = this.orderInProgressCmsModel.orderStatusColorRed.split(",");
const colorGreenCodes = this.orderInProgressCmsModel.orderStatusColorGreen.split(",");
const testObject = { colorName: "", colorCodes: []};
To populate the object with information, I have implemented the following steps:
testObj.colorName = "red";
testObj.colorCodes = colorRedCodes;
testObj.colorName = "green";
testObj.colorCodes = colorGreenCodes;
However, this method ends up only storing the last two sets of data due to overwriting.
The main objective here is to optimize the code by reducing the number of conditional statements (if's) and achieve the same outcome with a more logical approach if feasible:
if(colorRedCodes.some(s => s.includes(code))){
this.orderStatusColor = JSON.parse('{"color": "red"}');
}
if (colorGreenCodes.some(s => s.includes(code))){
this.orderStatusColor = JSON.parse('{"color": "green"}');
}
if (colorBlackCodes.some(s => s.includes(code))){
this.orderStatusColor = JSON.parse('{"color": "black"}');
}