GROQ's select
function works kind of like a JavaScript switch statement. You give it some conditions and a default and it will assign the first condition that passes to the new field you've created.
John Lindquist: [0:00] Let's add an age property to give us some more data to work with. I'm an old fart at 40 and we'll add in my son, who's a teenager at 15. We can easily set up a filter here. Inside of our array brackets, we can say, if age is less than 30, just grab all those people. That would only give back my son.
[0:23] Then, we get the query from last time, where his name is constructed from his first and his last name. We pluck off the name so that it's just an array of strings. It can be much more interesting to construct some additional data based on conditions. Inside of our object syntax here, we'll add a comma, just like JSON. We'll add a key here. We'll just call this group, and then use the SELECT function.
[0:50] SELECT is going to work on that age property. I'll just paste that back in. If age is less than 30, we can assign the group to the value of young, otherwise, we'll assign it to old.
[1:08] I'll go ahead and get rid of this name plucking so we can see the entire object. I'll run it again, and you can see we have me in a group of old and my son in a group of young. The SELECT function works like a switch statement, where we can add as many lines as we want here. The first match that it hits, it'll add that value to this key.
[1:30] If we put over 130, I could be super old, and then whatever other conditions based on age, or based on any of the values that we want to pass in there. I'm going to leave it with just the one condition and the default fallback, in case that condition isn't met.
[1:49] Then, from this, now that we have a group key, we could construct an entire sentence off of that. We could say, name + is + group, and then pluck that sentence off. We'll run this. You'll see we have two strings of, "John Lindquist is old," and, "Ben Lindquist is young," based on only this data set.
[2:13] At any time, you could come back and add more filtering, or more keys, or reconstruct the data in any way you want.