just a sanity check... If I'm looking to search i...
# cfml-general
t
just a sanity check... If I'm looking to search in an array for an item and return the item, arrayFind doesn't do that. It returns the index of the item. So I would need arrayFind and array[i]. I could return the item itself with arrayReduce, but that would loop over the the whole array no matter what. it wouldn't short circuit when the item was found. Or I could just do a for loop Is that right, or is there a function that I'm missing that does what I'm after?
a
So I would need arrayFind and array[i].
And? Looks like you might be trying to find a solution to a problem you might... not have..?
(to answer your Q though... no... nothing built in. And all the lovely array-iteration functions have the drawback you mention)
You could so something horrible with
Array.some
I guess.
t
Both of the below work. I just feel like this ought to be a one liner. but I guess not.
Copy code
public Thing function getThing(required string key) {
		var foundIndex = getThings().find(thing => thing.getKey() == key);
		if( foundIndex > 0 ) {
			return getThings()[foundIndex];
		}
		return new Thing();
	}

	public Thing function getThing(required string key) {
		for( var thing in getThings() ) {
			if( thing.getKey() == key ) {
				return thing;
			}
		}
		return new Thing();
	}
https://trycf.com/gist/bba300e362113135ca5c844d7dc6791f/acf2023?theme=monokai (for Lucee-only can use
nullValue
instead of that inline hand-cranked version, which works on both)
t
mm. I forgot about
?:
That would shorten it up.
✅ 1
a
Took me a few (awful) goes to try it
t
I've mostly taken to not using it at all because of https://tracker.adobe.com/#/view/CF-4212573, but in this case, it'll work fine.
🙃 1
a
Wow.
d
@Tim WTF, seriously.
t
3 days of my life trying to figure out wtf with that bug...
d
No kidding, props to you for that one. I don't see any confirmation from Adobe in the ticket. Wonky deluxe.