3

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:

enter image description here

Isn't there a CFML function that will allow me to check if "[email protected]" 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

1 Answer 1

5

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 == '[email protected]'
    })
){
    // do stuff if address is not used.
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 a writeDump() 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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.