Couldn’t figure this out for 7 days myself and w/ community, so may I ask you @Kirill_Leventcov for hints about how to filter among lists of services based on the property ‘token’ (ie: ‘stay’) of a child array for this result:
Click on ‘stay’ => 149 (2 options) | 199 (1 options)
Where services is your array, and chosenToken is the token the user chose.
You can further map the items object to suit your needs. Consider if the categories needs to be an array of objects or if just an array of texts is sufficient (you can FLATTEN the array if only objects with key “token” exist).
It’s 99% that!
I adapted to my schema and have: GROUP(SELECT(FLATTEN(MAP(data.ProvidersDBcount, item.services)), IS_IN_ARRAY_BY_KEY(FLATTEN(MAP(FLATTEN(MAP(data.ProvidersDBcount, item.services)), item.category)), "token", params.categParam)), item.price, {price: key + " ("+COUNT(items)+")"})
But it counts items (price 279) it should not. The formula gets tricked by this data schema where selected token ‘stay’ exists only in second array ‘category’.
Update: I suspect that FLATTEN “merges” these services lists and so 279 is now associated with the tokens this price should not be with. How do I overcome this?
Apologies, FLATTEN wasn’t the right formula function to use - in this case, it’d be easiest to MAP the categories property with PLUCK(item.categories, "token"). Then, you would use the IS_IN_ARRAY instead of IS_IN_ARRAY_BY_KEY as the SELECT filter.
Try opening the app (preview) and seeing if the formula works in action there. For me, the example results in the formula editor were showing some odd results at times, but everything worked perfectly fine in the app itself.
The FLATTEN is unnecessary unless you have a list of lists in your schema, I would not use it in this case. Also, if you want to preserve the categories being a list of objects instead of a list of strings, you don’t really need to PLUCK either - that’s a consideration for changing the entire schema. My initial proposed formula is still the one I suggest (with the added text formatting).
The SELECT formula takes two parameters: the array, and something that yields a true/false value to determine if this part of the original array should be included in the result. In this case, we want to determine if any of the items in the category property has the chosen token.
Let’s look at what’s going on with the SELECT and IS_IN_ARRAY(_BY_KEY) formulas step by step:
SELECT looks at the next (or first) item in the services array.
IS_IN_ARRAY_BY_KEY is evaluated for this item. In our case, it looks at the item.categories and sees if any of the tokens is equal to chosenToken.
a) If any token that matches chosenToken is found, the IS_IN_ARRAY_BY_KEY immediately exits and yields true.
b) If there’s no match, the IS_IN_ARRAY_BY_KEY yields false.
If the result of step 3 is true, the entire item is added to the result of the SELECT formula function. If it’s false, the item is ignored. In any case, we go back to step one until we’ve gone through the entire services array.
I hope this clear things up. I’m not sure about the URL parameter affecting the time it takes to display results, that shouldn’t be happening if everything is implemented properly.