Last active
October 17, 2019 06:05
-
-
Save pSapien/7ce1a4487bd8d84336b59cc097536eba to your computer and use it in GitHub Desktop.
a helper function to generate a new array starting from the index next to the lastItem
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
/** | |
* a helper function to generate a new array starting from the index next to the *lastItem* | |
* Eg: insertAtTheEnd(['a', 'b', 'c', 'd'], c) => ['d', 'a', 'b', 'c'] | |
*/ | |
function putAtTheEnd(arr, lastItem) { | |
const lastIndex = arr.findIndex(n => n === lastItem); | |
return arr.map((_, i) => { | |
const startIdx = (lastIndex + i + 1) % arr.length; | |
return arr[startIdx]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment