Created
May 14, 2021 10:20
-
-
Save bennadel/500c8b207da3ec9b9db452ce0496f9bc to your computer and use it in GitHub Desktop.
Using The Elvis / Null Coalescing Operator To Loop Past Array Boundaries In Lucee CFML 5.3.7.47
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
<cfscript> | |
letters = [ "A", "B", "C", "D", "E" ]; | |
linkedLetters = letters.map( | |
( letter, i ) => { | |
return([ | |
current: letter, | |
// We can use the null-coalescing operator to safely wrap around the | |
// bounds of an array. When the incremented / decremented index results | |
// in an undefined array item, it means that we've hit a boundary | |
// condition and we need to fallback to the item at the opposite end of | |
// the array (either the first or last item depending on the direction). | |
nextLetter: ( letters[ i + 1 ] ?: letters.first() ), | |
prevLetter: ( letters[ i - 1 ] ?: letters.last() ) | |
]); | |
} | |
); | |
dump( linkedLetters ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment