Last active
October 24, 2024 21:47
-
-
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
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 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