Set a value in a list of objects if condition is met

Options

I have a list of objects made like this:

[{restaurant_id: 2, entrance: 4}, {restaurant_id:3, entrance:5},{restaurant_id:4, entrance:6}..]

My goal is to edit (possibily using a conditional set filter, the list if restaurant_id equals a given number.

I know this could be done also through a FOR cicle or in other ways but I'm looking at the fastest way to do this.

Would be achievable through a filter?

Tagged:

Answers

  • evanwaller
    Options

    Federico,

    In my opinion, unless you have a few hundred thousand objects to sort through in your list, I think your best option would be to use a for loop because they are still exceptionally quick. I would loop through each object, compare the restaurant_id, and then alter your chosen value if the ID matches.

    If you want to continue searching for filtering options, I would recommend searching Xano Conditional Set Filters.

    Let me know if you run into any more issues.

    -Evan

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options

    Hi @Federico Somaschini, @evanwaller has the right idea. There are some tricks in Xano that you can use instead of a For Each Loop like the Array functions. In this case, you can use Array:Find First Element Index where restaurant_id = 6 and using that index, you can update the existing object array at that position. Here's a video on it: