Skip to content

Instantly share code, notes, and snippets.

@pSapien
Last active October 17, 2019 06:05
Show Gist options
  • Save pSapien/7ce1a4487bd8d84336b59cc097536eba to your computer and use it in GitHub Desktop.
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
/**
* 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