Help forum

Filtering results when the object key and path varies

  • adzar

    3 years ago

    I have a restaurant menu, which I want to filter based on a user's dietary requirements, so that they only see items which include none of their allergens or all those allergens can be removed if the chef modifies the dish.

    I have an API giving me a list of restaurant menu items (objects) - see image attached.

    If a menu item from the API contains a given allergen, the object includes a key named after the allergen populated with either 'y', or '[modification description]', where [modification description] is a text string. If it does not contain a given allergen, there is no key for it.

    The variable 'selected-allergen' is an array of user selected allergens. e.g. ["gluten", "milk"].

    I created menu-item as a component, which repeats over items from the API to display the restaurant's menu.

    I want to:

    A - filter menu items from the API:
    Does the menu item from the API contain any keys that match the 'selected-allergen' items?
    If true, is the value of any of these keys "y"?
    If true, exclude the menu item
    Else, include the menu item (in this case, the item would have a [modification description] under one or more 'selected-allergen' keys)
    Else, include the menu item

    B - display a list of modification descriptions for each menu-item:
    Display a list of any [modification description]s under keys that match the 'selected-allergen' items

    I am really stuck on how to achieve the filter in A, because so far I have only learned how to create a formula based on the end value and for a given key in an object. In this scenario, I need to check if the key is present and then potentially evaluate multiple keys.

    I'm not sure if I will be able to figure out B when A is in place. But I'm guessing it would be some sort of repeat-item over the keys that match 'selected-allergen'.

    Any advice would be greatly appreciated 🙏
    1151081729520582676-Screenshot_2023-09-11_at_20.06.10.png
  • Tod

    3 years ago

    Great energy @adzar! Your continuous contribution to the toddle Community just made you advance to Community Level 4!
  • gazinhio

    3 years ago

    You'll have to use the Filter formula to generate a list that includes only the items that you want. Here's a thread that you might find useful : https://discord.com/channels/972416966683926538/1130812696070193152
  • gazinhio

    3 years ago

  • adzar

    3 years ago

    Thanks, I'll check it out! 👍
  • adzar

    3 years ago

    @ thanks for the resource - I have this functionality already, although interestingly I achieved it with 'Delete' and 'Index of' rather than filter.

    Unfortunately though I'm still stuck on my current problem as I think it's a little more complex. I'm not searching through an array, I'm searching through an object for specific keys and values.
    1151104938533978152-Screenshot_2023-09-12_at_11.37.19.png
  • gazinhio

    3 years ago

    Ultimately you're dealing with an array of objects from your API and you want to filter out certain items/objects in that array based on the value of certain properties. This is exactly what Filter is designed to do. The great thing about Filter is that it has a Formula input which enables you to define another formula which iterates (loops) over every item in the source array. In that iteration formula you can get as complex as you need to determine whether an object is included in the result array, just return true or false from the formula to determine which items/objects get included.
  • adzar

    3 years ago

    Ok I see, I need to unlock the power of the filter 💪 . I'll give it another go, thanks!
    👍1
  • gazinhio

    3 years ago

    There are three formulas in Toddle related to arrays that once you get them, there are no limits. These are Filter, Map and Reduce. Filter, I explained above, Map lets you transform the data in an array, for example you start with your API data and you want a simplified array with just certain properties to work with in your app, then Map is the one to use, and Reduce is way to "reduce" an array to a single value, typical examples are for calculating the sum or count of values in a list but also great for building up a string from values in the array. Also Get let's you get deep into an array and the objects in the array to pull out a single value and use Set to do the opposite, change a single value buried deep in an array etc. Get your head around those and you'll be ripping up trees in no time! 💪
  • adzar

    3 years ago

    Holy moly 😅
  • adzar

    3 years ago

    I'm trying to break it down step-by-step, so firstly:
    For each item in menuItem, check if it contains any keys that match the selectedAllergens using the filter function. If not, then include it in the list. But I'm still struggling...
    https://www.loom.com/share/0325ccd8704e43758c3e1e6110ef07af?sid=721891d1-5e13-4486-b081-087f1aa00f4a
  • gazinhio

    3 years ago

    Is "Gluten" a field that is always returned for every record and is either true or false, or is it only included as a "field" for records where Gluten is some form of tag against the record?
  • adzar

    3 years ago

    The latter. For example, this lasagna item does not include Gluten, but it does contain other allergens "Milk" and "Eggs"
    1151138212274770050-Screenshot_2023-09-11_at_18.04.28.png
  • gazinhio

    3 years ago

    OK, that makes it a bit more awkward cos you'll need to check on every potentiality of tags, I take it there's a predefined/finite number of these things to check against?
  • Tod

    3 years ago

    Great energy @! Your continuous contribution to the toddle Community just made you advance to Community Level 9!
  • adzar

    3 years ago

    Yep, 14 allergens (tags)
  • gazinhio

    3 years ago

    If "Eggs" is "n", does it still return Eggs as a field, or is the fact that it's there indicate that it will always be a "y"?
  • adzar

    3 years ago

    So if it's there, it means that it's either "y", or a "[modification description]". Where [modification description] is a string for example: "tofu scramble instead of eggs". This description would be different depending on the dish.
  • adzar

    3 years ago

    I can change how the API is returning the data if it simplifies things. For example, I could return the field "Allergens", which has an array e.g. ["Gluten","Eggs"]
  • This might be ok for my MVP - I can leave out the modification stuff for now
  • Then I can just say, do any of the values in "Allergens" match any of the values in "selectedAllergens". If yes, exclude from list
  • gazinhio

    3 years ago

    Yeah, that's how I'd normally expect to see the data structured.
  • gazinhio

    3 years ago

    That way you can easily check against one field rather than having to manually to check against 14 different values to 1) check if it exists and 2) check if it's in the other array
  • gazinhio

    3 years ago

    I don't think you need to leave the modifications out, you just need to find a better to structure it at the database level.
  • And I'll say that the having it in different fields, one for each type of allergen that may or may not be returned from the database, it's not that it's difficult to manage just in Toddle, you'd have a real job managing that in anything you could use.
  • Andreas Møller

    3 years ago

    @svenning do we need to introduce a level above toddle master?
  • svenning

    3 years ago

    It sure seems like we have to 😃
  • gazinhio

    3 years ago

    Well, in the Bubble community we have people referred to as "top 1% Bubblers" however that title must be self-professed, it cannot be ordained 😆
    Thanks guys but the only level above "Toddle Master" should only ever be "Toddle Founder" and, in the words of Highlander, there can only be 2!
  • Andreas Møller

    3 years ago

    Fair point
  • adzar

    3 years ago

    Thanks @ for the advice. I've updated my data source to a result like this: "Allergens": ["Gluten", "Milk"].

    How can I check if any values in "Allergens" match any values in "selectedAllergens"?
  • I'm not sure if you saw my DM, but I appreciate you're offering a lot of help here and I'm happy to pay for your time!
  • adzar

    3 years ago

    This was my attempt, but it seems that "Includes" only works if the input item is a string that matches a value in an array
    1151176404558422036-Screenshot_2023-09-12_at_16.21.29.png
  • gazinhio

    3 years ago

    No didn't get your DM, did you send it here on Discord? Happy to help on here as much as I can, need people learning, developing and mastering Toddle.
    💪1
  • gazinhio

    3 years ago

    Try it the other way around eg selected-allergen -> Includes -> Item/fields/allergens
  • adzar

    3 years ago

    Yep on Discord
  • gazinhio

    3 years ago

    Yeah sorry, it had dropped into Spam. Got it now.
  • adzar

    3 years ago

    No luck 😦
  • gazinhio

    3 years ago

    Yeah I read the data wrong, that obviously wouldn't have worked 🤦‍♀️ . Let me have a little play and get back to you.
    🙏1
  • There was no Javascript in the video, it's all Toddle so ignore the AI generated Loom title!
  • 1151195346421153885-intersect-formula.png
  • gazinhio

    3 years ago

    Who said you can't do nested loops in Toddle?!
    🔥2
  • adzar

    3 years ago

    Glory to @ 🙏
    Thank you, thank you, thank you... this is next level!!
    🤓1
  • adzar

    3 years ago

  • adzar

    3 years ago

    It was because in my "Equals", I just typed "0", which meant it considered it a string, rather than choosing number->0
    👍1
  • adzar

    3 years ago

    Ah, interestingly, if a menuItem has no allergens, then it is excluded from the list. I guess there must be some sort of error, because it does not have the Allergen path. As a work around for now, I will just populate Allergens with "None" in the database 😉
  • Tod

    3 years ago

    Great energy @adzar! Your continuous contribution to the toddle Community just made you advance to Community Level 5!
  • gazinhio

    3 years ago

    This'll solve that one for you.
    1151263345610063962-intersect-formula2.png
  • gazinhio

    3 years ago

    Ah I see, if there are no allegens for a menu item then it doesn't even return an allergen array!?
  • gazinhio

    3 years ago

    Do this for the Or condition instead and it should solve it for you.
    1151267460394262568-intersect-formula3.png
  • BTW you have completely sold me on never using AirTable! Never seen anything like it!
    🤦‍♂️1
  • adzar

    3 years ago

    Thanks!
  • adzar

    3 years ago

    Another big thanks to @ for all the help with this. Today I used the same approach to program the diet filter - which looks for dishes that include the selected tags with some extra logic based on items tagged as vegan also being suitable for vegetarians.

    Here's the explanation in case it helps anyone in the future.
    https://www.loom.com/share/c96b65a6440c4693842cf7256af71235?sid=e85a2f5d-7f04-42bd-b5de-4a0fa1d40fca
    🙌2
  • gazinhio

    3 years ago

    Bravo! 👏 Now you're down the rabbit hole. It's got it's hooks into you now and there's no way back 😆 🤓
    😂1
    🙃1