Зависит от того, будете ли вы использовать switch
или if
, но здесь важнее читаемость кода.
Если бы я правильно понял ваш псевдокод, я бы сказал что-то вроде этого:
// Your questions - list of question object.
const questions = [
{
id: 'someValue1',
question: "First Question ?"
},
{
id: 'someValue10',
question: "Tenth Question ?"
},
{
id: 'someValue12',
question: "Twelfth Question ?"
},
{
id: 'someValue22',
question: "Twenty two Question ?"
}
];
// Your tasks object.
const task = {
first: () => {
console.log("First task performed");
},
tenth: () => {
console.log("Tenth task performed");
},
nth: () => {
console.log("Nth task performed");
},
custom_group_task: () => {
console.log("Custom task performed");
},
custom_group_task2: () => {
console.log('Custom task2 performed');
},
default_one: () => {
console.log("Default task performed");
}
};
const getGroupTask = id => {
// Define groups
const group1 = ['someValue11', 'someValue12'];
const group2 = ['someValue21', 'someValue22'];
if(group1.includes(id)) {
return task.custom_group_task;
}
else if(group2.includes(id)) {
return task.custom_group_task2;
}
return false;
};
// Create task
const taskFactory = id => {
const groupTask = getGroupTask(id);
if(groupTask !== false) {
return groupTask;
}
switch(id) {
case 'someValue1':
return task.first;
case 'someValue10':
return task.tenth;
case 'someValue11':
case 'someValue12':
return task.nth;
default:
return task.default_one;
}
};
// Your index
questions.forEach(e => {
const taskResult = taskFactory(e.id)();
});
Просто организуйте это в файлы с четко определенными задачами и вопросами и импортируйте их.