Last active
February 6, 2019 18:53
-
-
Save kevinsalter/0f7a45aca036ba13ada1fd53b8330b87 to your computer and use it in GitHub Desktop.
naive array re-ordering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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