PricingBlog

Filtering results when the object key and path varies

  • adzar_97016-1151081729189224498

    adzar

    2 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-1151081732318179398

    Tod

    2 years ago

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

    gazinhio

    2 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-1151085007302045726

    gazinhio

    2 years ago

  • adzar_97016-1151089459962576916

    adzar

    2 years ago

    Thanks, I'll check it out! 👍
  • adzar_97016-1151104938844368968

    adzar

    2 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-1151106616565969008

    gazinhio

    2 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_97016-1151106948142469170

    adzar

    2 years ago

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

    gazinhio

    2 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_97016-1151115140859707452

    adzar

    2 years ago

    Holy moly 😅
  • adzar_97016-1151133503367417956

    adzar

    2 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-1151137269844037784

    gazinhio

    2 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_97016-1151138212467724519

    adzar

    2 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-1151142417089691688

    gazinhio

    2 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-1151142418368962662

    Tod

    2 years ago

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

    adzar

    2 years ago

    Yep, 14 allergens (tags)
  • gazinhio-1151142954174533643

    gazinhio

    2 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_97016-1151143396971397251

    adzar

    2 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_97016-1151143687280132210

    adzar

    2 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-1151144179158761483

    gazinhio

    2 years ago

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

    gazinhio

    2 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-1151145689234030712

    gazinhio

    2 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.
  • andreasmoller-1151161783407607898

    Andreas Møller

    2 years ago

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

    svenning

    2 years ago

    It sure seems like we have to 😃
  • gazinhio-1151169223918374953

    gazinhio

    2 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!
  • andreasmoller-1151170416631619604

    Andreas Møller

    2 years ago

    Fair point
  • adzar_97016-1151175078726348850

    adzar

    2 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_97016-1151176404835254365

    adzar

    2 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-1151177147365466173

    gazinhio

    2 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-1151177771607937114

    gazinhio

    2 years ago

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

    adzar

    2 years ago

    Yep on Discord
  • gazinhio-1151178253726400704

    gazinhio

    2 years ago

    Yeah sorry, it had dropped into Spam. Got it now.
  • adzar_97016-1151178491610529863

    adzar

    2 years ago

    No luck 😦
  • gazinhio-1151178876039479437

    gazinhio

    2 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-1151195659031027752

    gazinhio

    2 years ago

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

    adzar

    2 years ago

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

    adzar

    2 years ago

  • adzar_97016-1151217767303880774

    adzar

    2 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_97016-1151226468848898209

    adzar

    2 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-1151226471080284261

    Tod

    2 years ago

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

    gazinhio

    2 years ago

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

    gazinhio

    2 years ago

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

    gazinhio

    2 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_97016-1151270394284740719

    adzar

    2 years ago

    Thanks!
  • adzar_97016-1151522081335431281

    adzar

    2 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-1151529769398186064

    gazinhio

    2 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