I have a query with ID and Name columns, that I wa...
# cfml-general
d
I have a query with ID and Name columns, that I want to "reduce" to a struct whose keys are those IDs and values are the corresponding names. Old school would be to create a struct then loop over or queryEach() the query setting values in it. Nothing wrong with that, just wondering if there's a more modern idiomatic approach. Could do something like this:
Copy code
values = queryReduce(qry, function(result={}, row)
{
	result[row.ID] = row.Name;
	return result;
});
The 2-line function setting the result key value then returning it seems awkward. Is there a cleaner way?
j
The way I would do this is a simple loop over the query
Copy code
values = {};
for (var row in qry) {
	values[row.ID] = row.Name;}
a
@Dave Merrill it's always gonna be easier for ppl to help you if you provide more code to work with. Like some sample data. As it stands for me to answer yer question thoroughly, I'm gonna have to mess about creating a query to test with etc. I mean I'm going to, but it's a "barrier to entry".
If you had presented something like that, with the question "like that, except using a one liner", it's leaving the answerer a lot less work, as well as it being a lot more clear what yer asking / expecting.
d
@JohnT Yes that's the old-school version I mentioned, just wondering if there's a new-school equivalent I'd like better, along the lines of result = someExpression(qry, function() {...})); @Adam Cameron Fair enough, just thought some new-school ninja might have such a thing on the tip of their tongue.
a
I dunno about new-school ninjas, but this crusty old dork likes to present a complete, tested, portable, mutable solution when answering questions. And the more context that has been provided in code to start with, the better. Did you look at my solution, and did it work for you?