Copy link to clipboard
Copied
How do you break out of the arrayEach function - stop execution?
break acts like continue
Code
<cfscript>
myArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
writeoutput("<h2>Loop</h2>");
for ( counter=1; counter<=arrayLen(myArray); counter++ ) {
thisElement = myArray[counter];
if ( counter == 5 ) {
continue;
}
if ( counter == 8 ) {
break;
}
outString = counter & " : " & thisElement & "<br>";
writeOutput(outString);
};
writeoutput("<h2>ArrayEach</h2>");
counter = 0;
arrayEach( myArray , function( thisElement ){
counter += 1;
if ( counter == 5 ) {
continue;
}
if ( counter == 8 ) {
break;
}
outString = counter & " : " & thisElement & "<br>";
writeOutput(outString);
});
</cfscript>
Output
Loop
1 : 1
2 : 2
3 : 3
4 : 4
6 : 6
7 : 7
ArrayEach
1 : 1
2 : 2
3 : 3
4 : 4
6 : 6
7 : 7
9 : 9 this should not be here
Answer would be much appreciated, no documentation.
Hi martiehen2,
One key consideration: we should, optimally, use language features as documented. By that I mean, as they are designed to be used and according to the official documentation. This is especially relevant in a weakly-typed language like ColdFusion.
Accordingly, in ColdFusion, break;/<cfbreak> and continue;/<cfcontinue> are designed to be used in ordinary loops, not in iterative loops such as arrayEach() and structEach(). So, even if break; or continue; worked in an iterative loop
...Copy link to clipboard
Copied
arrayEach doesn't have a break out. Much like a forEach loop, it iterates over everything. The only way to break out is to throw an exception. Same for .map() or arrayMap(). Or so I understood. Didn't think a continue would work within an arrayEach, but apparently it does.
If you want something that you can break out of, a for loop is probably best.
V/r,
^ _ ^
Copy link to clipboard
Copied
It's exactly as WolfShade says. The each means the closure function will be executed for each element in the array.
Copy link to clipboard
Copied
However, and I am not sure this would do what you want.. but.. since continue will 'skip' an iteration.
writeoutput("<h2>ArrayEach</h2>");
counter = 0;
arrayEach( myArray , function( thisElement ){
counter += 1;
if ( counter == 5 ) {
continue;
}
if ( counter >= 8 ) {
continue;
}
outString = counter & " : " & thisElement & "<br>";
writeOutput(outString);
});
This would at least kind of, almost, act like a break in that it prevents any further processing by skipping the processing in the rest of the iterations. Just a thought.
HTH,
^ _ ^
Copy link to clipboard
Copied
Hi martiehen2,
One key consideration: we should, optimally, use language features as documented. By that I mean, as they are designed to be used and according to the official documentation. This is especially relevant in a weakly-typed language like ColdFusion.
Accordingly, in ColdFusion, break;/<cfbreak> and continue;/<cfcontinue> are designed to be used in ordinary loops, not in iterative loops such as arrayEach() and structEach(). So, even if break; or continue; worked in an iterative loop, I wouldn't use it. I would think of it as suboptimal and risky, what is usually called 'undocumented'. Much like isDate('1a') or isDate('2p'), which return True.