this might have been asked before, but didn't quite find... or what I found was old.
I have an array of structures like so:
Isn't there a CFML function that will allow me to check if "test@email.com" already exists under any of the structures "toAddress" ? I can obviously loop my array, check and break if found, but wondering if something already exists for this ?
Thank you. Pat
You can use arrayFind()
with a callback function. Using your data structure above and assume the array is named myArray
if(
!arrayFind( myArray, function( item ){
return item.toAddress == 'test@email.com'
})
){
// do stuff if address is not used.
}
Works! Thanks. One question though: this means however that it'll still loop through all the array element even if it found a match, correct ? If so, is there any way I could tell the arrayFind() to break if function returned true ?
arrayFind()
returns the first index that matches the logic you provide in the callback. I am not sure of the internal logic, but it seems it would break after the first element found. A way to test this would be to add awriteDump()
to dump the email in the callback to see if it outputs all of them.For future readers:
arrayFind()
will stop looking once it finds the 1st instance. It's a fairly efficient function for doing this. trycf.com/gist/af2e5fe9253fab408c6c21cfa490a6ae/…Awesome! Thanks for your time and help :)