This question is related to this and this questions.
I have an array like so.
const questions = [
{ _id: 1, q: 'why?', group: 'no-group', date: '8', selected: false },
{ _id: 2, q: 'what?',group: 'no-group', date: '6', selected: false },
{ _id: 3, q: 'when?',group: 'no-group', date: '7', selected: false },
{ _id: 4, q: 'where?',group: 'no-group',date: '5', selected: false },
{ _id: 5, q: 'which?',group: 'no-group',date: '3', selected: false },
{ _id: 6, q: 'who?', group: 'no-group', date: '0', selected: false },
{ _id: 7, q: 'why not?', group: 'no-group', date: '9', selected: false },
{ _id: 8, q: 'who, me?', group: 'no-group', date: '4', selected: false },
{ _id: 9, q: 'where is waldo?', group: 'no-group', date: '1', selected: false },
{ _id: 10,q: 'which way is up?', group: 'no-group', date: '2', selected: false },
{ _id: 11,q: 'when is lunch?', group: 'no-group', date: '10', selected: false },
];
// function takes an array of selected questions as an argument
function groupSelectedQuestions(selectedQsIds) {
let newQuestions = questions.map(question => {
if (selectedQsIds.length == 1 && selectedQsIds.includes(question._id)) {
question.group = 'no-group';
return question
} else
if (selectedQsIds.length > 1 && selectedQsIds.includes(question._id)) {
question.group = 'group 1';
return question
} else {
return question;
}
})
return newQuestions
}
console.log(groupSelectedQuestions([1, 2, 5]))
From the code above you can group and ungroup questions.
I have a number of conditions on how I want to add the group property value to 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 order of the array should remain the same as the original.
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.
So every time the groupSelectedQuestions() function is called the group property in the affected questions should be updated accordingly.
For example if I questions of _id 1, 2 and 3 are selected and grouped. They should have the key value: group: "group 1". Then, if the questions of _id 4, 5 and 6 are selected and grouped they, the newly grouped 3 questions, should have the key value: group: "group 1" and the earlier grouped questions should have the key value: group: "group 2". Because the new group 1's earliest date is earlier then the new group-2's earliest date.
Basically I am trying to achieve what was done by this answer without bundling the arrays in objects and without changing the order of the array.
dateproperty values are strings. Should they be ordered in string lexical order? Or are they really numbers and to be ordered numerically?