What's the right way to reach a certain path across array records?

Options
Hello!
I am trying to run a condition that would check all array records "event_type" value to see if it equals a string.

My Array is
{
  "resource_user_events": [
    {
      "id": 4,
      "created_at": 1641609301338,
      "user_id": 1,
      "event_type": "Like",
      "related_resource": "Activity",
      "related_resource_id": 2
    },
    {
      "id": 2,
      "created_at": 1641604856251,
      "user_id": 1,
      "event_type": "View",
      "related_resource": "Activity",
      "related_resource_id": 2
    }
  ]
}

In Xano, if I try something like resource_user_events.0.event_type, it would check against a single record.

I was trying to use a JSON Syntax like resource_user_events.*.event_type but I don't think that it is supported.

How would I do this? Do I need to run a loop? I tried briefly to use a loop but then noticed conditions were interfering with one another incorrectly.

Thanks![Screen Shot 2022-01-09 at 7.17.46 PM.png]

Comments

  • Mitch
    Mitch Member
    Options
    I can't say whether this is the easiest way, but you could do this:
    1.  run a loop.  
    2. if "events" was your loop "as" variable, then run a conditional in the loop.  
    3. For the value to check, use events.event_type (setting it as "events" the variable... which I always forget and is a pain in the ass).  

    It wasn't clear if you were trying to check whether the value "is a string" or is a particular value. If it's the first, then add a "is_text" filter, which will spit out a boolean.  So the conditional would be equals true/false.  If you're checking a specific value, then obviously ditch the filter and put the value you're checking in the second conditional field.

    Hope that helps
  • Abdulrahman Hariri
    Options
    Thank you for your reply Mitch.
    It is strange because I did exactly like you've mentioned. However, I noticed that, for unknown reason, the conditions were influencing each other when there are multiple records in the area. However, when there was only 1 record, it was working fine.

    I couldn't really figure out what was wrong so I removed the loop and used a "Has any" or something similar function twice.

    Adding a filter for is_text (boolean) was something new for me, thanks a lot for suggesting that!

    Thanks a lot!
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     The array functions are sort of short cuts to loops. So the Has Element and checking if it is text sounds like it worked for you.

    However, the loop method should definitely still work. Were you using a For Each Loop and the correct variables?
  • Abdulrahman Hariri
    Options
    [Screen Shot 2022-01-12 at 3.14.24 PM.png]Thanks . I've cloned the endpoint to have another attempt at this and results are still confusing. Attached is the whole function stack (Loop with 2 conditions for each loop where I am checking event_type = View and event_type = Like to create the relevant variable as true or false (if it doesn't exist).
  • Abdulrahman Hariri
    Options
    However, when I get the results, they see to be off a bit.
    As you can see from the debug, a like event and a view event exists. However, for some reason, is_viewed & is_liked results are not consistent. One of them is true while the other is false.

    Could it be that I am causing an issue in the logic despite having 2 separate IF conditions?

    Thanks![Two Events on Post.png]
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     - right now you are creating is_viewed and is__liked in the For Each Loop, so you're only going to be returning the results for the last item... which is why you're only seeing results fo the last object... what are you trying to accomplish with is is_viewed and is_liked?

    We also have office hours where you can get hands-on help. Link to sign up is on the left side of the community. 
  • Abdulrahman Hariri
    Options
     Thanks for your response!
    Basically there can be a maximum of 2 events per post for the current user. A post could have a view, like, or both for the current user (Max of one for each!).

    Giving that there can only be a max of 1 event meeting the filters I am checking for, I assumed that for loop would be sufficient.

    Was I wrong in making this assumption?
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     - I'm still a little unclear. I see two objects in your array... are you just trying to check for existence within the array? If so, you could skip the loop and conditionals and make it a whole easier by leveraging the Array: Has Any Element function
  • Abdulrahman Hariri
    Options
     Exactly, if a view obj exists, is_viewed should be true. Similar situation for is_liked. If Has any is the more appropriate option, that's great & thanks for confirming! I just want to make sure I am following the better approach since I am new to Xano 🙂.

    Again, many thanks for your time! I will also be sure to join some office hours to learn from other examples too.