Skip to content

Instantly share code, notes, and snippets.

@kevinsalter
Last active February 6, 2019 18:53
Show Gist options
  • Save kevinsalter/0f7a45aca036ba13ada1fd53b8330b87 to your computer and use it in GitHub Desktop.
Save kevinsalter/0f7a45aca036ba13ada1fd53b8330b87 to your computer and use it in GitHub Desktop.
naive array re-ordering
const getReorderedQuestions = (event, questions) => {
const movedQuestion = questions.find((question, index) => index === event.oldIndex);
const remainingQuestions = questions.filter((question, index) => index !== event.oldIndex);
const reorderedQuestions = [];
remainingQuestions.forEach((question, index) => {
if (index === event.newIndex) {
reorderedQuestions.push(movedQuestion);
reorderedQuestions.push(question);
} else {
reorderedQuestions.push(question);
}
});
if (event.newIndex === remainingQuestions.length) reorderedQuestions.push(movedQuestion);
return reorderedQuestions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment