I have this array of object:
let choices = [
{title: 'Single Card Payment', func: 'singleCard'},
{title: 'Monthly Card Payment', func: 'monthlyCard' },
{title: 'Monthly Direct Debit Payment', func: 'monthlyDD'}
]
I'm looping through with it and I would like to call functions with the value of the func, like this (which is obviously wrong)
for(choice for choices) {
choice.func('some', 'params');
}
You can create an object with methods. Like below
const funcs = {
singleCard: () => { console.log('single card called ')}
}
And then call like this
for (i in choices) {
funcs[choices[i].func]()
}
using evil function:
let choices = [
{title: 'Single Card Payment', func: 'singleCard'},
{title: 'Monthly Card Payment', func: 'monthlyCard' },
{title: 'Monthly Direct Debit Payment', func: 'monthlyDD'}
]
for(i in choices) {
eval(choices[i].func)('some', 'params');
}
edit: it's eval guys not evil. 0K.