在结构数组中查找值
这可能之前有人问过,但没有完全找到......或者我发现的是旧的。
我有一个像这样的结构数组:
是不是有一个 CFML 函数可以让我检查“test@email.com”是否已经存在于“toAddress”的任何结构下?我显然可以循环我的数组,如果找到则检查并中断,但想知道是否已经存在某些东西?
谢谢你。拍
回答
您可以arrayFind()与回调函数一起使用。使用上面的数据结构并假设数组已命名myArray
if(
!arrayFind( myArray, function( item ){
return item.toAddress == 'test@email.com'
})
){
// do stuff if address is not used.
}
- `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.