I've got datas from an external source that uses initials to refer to specific type of data
| Initial | Value |
|---|---|
| A | Attelé |
| AB | Abajour |
| B | Base |
| ... | ... |
I want to be able to get back the value. As I know the value for each shortcuts, I could do something like that :
function getValue(initial)
{
if(initial == "A") return "Attelé";
else if(initial == "AB") return "Abajour";
else if(initial == "B") return "Base";
}
But this is not a good practice as I will have to add a new comparaison for every initial
So i'm thinking about using two array as references :
var values = ["Attelé","Abajour","Base"];
var initialList = ["A", "AB","B"];
function getValue(initial)
{
var index = initialList.indexOf(initial);
if(index > -1) return values[index];
else return null;
}
console.log(getValue("A"));
But here again, i think there is a better practice. To add a new initial, I still have to modify two variables, and if my arrays's length are not matching I could have an issue with the result. I would like to be able to change only one variable.
Hence my question : How can I use one object/array/table to match my initials, and what is the best practice to look at ?
Can you not just put the data in an object
const names = {
"a": "Attelé",
"b": "Base"
}
and then access the data by using
names["a"]