Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as JavaScript by wtruong ( 7 years ago )
/**
* Converts our list of questions to an object of groups that contain a list of questions.
*
* ex. of questions list shape:
* questions = [
* {
* id: 1,
* subGroupId: 1,
* type: "Vote_Image",
* image: {
* id: 61221208
* },
* maximumOptionsRequired: 0,
* minimumOptionsRequired: 1,
* options: [
* {
* id: 1,
* value: 123456,
* text: 'true'
* },
* {
* id: 2,
* value: 123457,
* text: 'false'
* }
* ]
* }
* ]
*
* ex. of groups list shape:
* groups = [
* {
* subGroupId: 1
* type: "Vote_Image",
* questions: [...]
* }
* ]
* @param questions An array of question objects.
*/
convertQuestionsListToGroupsObject = (questions) => (
questions.reduce(this.questionsListToGroupsListReducer, [])
);
/**
* Reducer function that converts a list of questions into a list of unique groups that contain a list of questions.
*
* @param groups
* @param question
* @returns {*}
*/
questionsListToGroupsListReducer = (groups, question) => {
const existingGroupObject = groups.find((group) => (group.subGroupId === question.subGroupId));
if (existingGroupObject) {
existingGroupObject.questions = [...existingGroupObject.questions, question];
// Replace the original group object with an updated version that contains the newly added question.
return groups.map(group => existingGroupObject.subGroupId === group.subGroupId ? existingGroupObject : group);
}
const newGroupObject = {
subGroupId: question.subGroupId,
type: question.type,
questions: [question]
};
return [...groups, newGroupObject];
};
Revise this Paste
Parent: 98515