groupSelectedQuestions(selectedQuestions){
var questions = [
{ q: 'why?', group: 'no-group', date: '1' },
{ q: 'what?', group: 'group 1', date: '1' },
{ q: 'when?', group: 'group 2', date: '2' },
{ q: 'where?', group: 'group 1', date: '2' },
{ q: 'which', group: 'group 2', date: '3' }
],
result = questions.reduce(function (r, a) {
r[a.group] = r[a.group] || [];
r[a.group].push(a);
return r;
}, {});
/**
more code here.
Here I would put if statements that check for all condtions
I have stated in the question below
*/
}
I am trying to make a function that users call and groups questions in certain groups. Above you can see part of the code I have come up with.
I have a number of conditions on how I want to group the questions.
- No group can have less than two number of questions.
'no-group'means the question is not in any group. So'no-group'can be just one question or all of them.- Groups should be assigned by the earliest date in the questions of that group. For example
'group 1'earliest(in terms of date) question should be earlier than the earliest question in 'group 2'and this should also be readjusted when questions are regrouped or if a question is removed from a group. - Grouped questions can be regrouped. And when doing so if any question is left in a group alone it should marked as
'no-group'. - When assigning groups when
'group 1'is taken assign'group 2', when'group 2'is taken assign'group 3'and so on.
The way I would do it is use if statements. But since the array of questions can have up to twenty questions and the groups can go from 'group 1', 'group 2'... to 'group 20', the number of if statements will become many.
I made a stackblitz to communicate better what I am trying to achieve. Is there a way I can use recursion to achieve what I want to achieve and avoid many if statements?
If there is something that is not clear kindly ask I will be glad to make it clear.
The code in the stackblitz is as follows (it is an Angular stackblitz):
the controller
questions = [
{ _id:1, q: 'why?', group: 'no-group', date: '1', selected:false },
{ _id:2, q: 'what?', group: 'group 1', date: '1', selected:false },
{ _id:3, q: 'when?', group: 'group 2', date: '2', selected:false },
{ _id:4, q: 'where?', group: 'group 1', date: '2', selected:false },
{ _id:5, q: 'which?', group: 'group 2', date: '3', selected:false }
];
selectOrUnselectQuestion(question){
let newQuestions = this.questions.map(newQuestion => {
if(newQuestion._id === question._id){
if(!newQuestion.selected){
newQuestion.selected = true;
} else {
newQuestion.selected = false;
}
return newQuestion;
} else {
return newQuestion;
}
})
this.questions = newQuestions;
}
groupSelectedQuestions(){
let selectedQuestions = this.questions.filter(q => q.selected);
let selectedQuestionIds = selectedQuestions.map(selectedQuestion=>{ return selectedQuestion._id; })
let newQuestions = this.questions.map(question => {
if(selectedQuestions.length==1 && selectedQuestionIds.includes(question._id)){
question.group = 'no-group';
question.selected = false;
return question
} else
if(selectedQuestions.length>1 && selectedQuestionIds.includes(question._id)){
question.group = 'group 1';
question.selected = false;
return question
} else {
return question;
}
})
this.questions = newQuestions;
// deselect selected questions
}
the view:
<div style="text-align:center">Questions</div>
<div style="text-align:center; padding:10px;">
<div *ngFor="let question of questions" (click)="selectOrUnselectQuestion(question)"
[ngClass]="{'selected': question.selected}" class="question">
<span style="padding-right:10px">{{question.q}}</span>
<span>{{question.group}}</span>
</div>
<button (click)="groupSelectedQuestions()" style="margin:10px 0" type="button">
group selected questions
</button>
</div>
lodashgroupBy function for that