Any way to replace this switch statement for object?
Object.keys(data).forEach(key => {
switch (true) {
case key === '1':
Store.get1();
return;
case key === '2':
get2();
return;
case key === '4':
get4(4);
return;
case key === '5':
get5({value1 = 5.value1, value2 = 5.value2});
return;
default:
break;
}
})
to replace it with something like this:
Object.keys(data).forEach(key => {
const keyMap = {
1: Store.get1(),
2: get2(),
3: get3(),
4: get4(),
5: get5({value1 = 5.value1, value2 = 5.value2}),
};
return keyMap[key];
but this way every function in keyMap just gets triggered every time.
Syntax errors in the original aside, just have the functions in the lookup table, then call them:
const keyMap = {
1: Store.get1,
2: get2,
3: get3,
4: get4,
5: get5,
};
Object.keys(data).forEach(key => {
return keyMap[key]();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With