Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created May 14, 2021 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/500c8b207da3ec9b9db452ce0496f9bc to your computer and use it in GitHub Desktop.
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
<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