Skip to content

Instantly share code, notes, and snippets.

@bennadel

bennadel/test.cfm

Created May 14, 2021
Embed
What would you like to do?
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