Skip to content

Instantly share code, notes, and snippets.

@AliAlmasi
Last active October 24, 2024 21:47
Show Gist options
  • Save AliAlmasi/fcdef741bb336cbe2b3f9155334adf3f to your computer and use it in GitHub Desktop.
Save AliAlmasi/fcdef741bb336cbe2b3f9155334adf3f to your computer and use it in GitHub Desktop.
A simple function to help you access array values from the end. Source: https://stackoverflow.com/a/64296726
const nIndex = (array, nIndex) => array.slice(nIndex)[0];
// Example:
const arr = [1,2,3,5,7,9];
console.log(nIndex(arr, -2)); // output: 7
console.log(nIndex(arr, -4)); // output: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment