How can i find all elements in array and remove them?

Options
Stefan Klein
Stefan Klein Member ✭
edited June 2023 in ? Help! I'm a Noob

Hi Community,

I'm stuck (again πŸ™‚). I have an array of objects (royalties_per_book). I use the Find All Elements function to find all objects matching my expression and store them in another array (publisher_aggregated).

After that, I want to delete the found objects from the first array but I can't figure that out. I tried to use the diff_assoc filter, but that seems to do nothing. I also tried to use a second "Find All Elements" combined with an "in" search filter (as shown here), but I think that doesn't work for objects.

Any advice?

The array structure:

The Loop:

Best,
Stefan

Best Answer

  • Stefan Klein
    Stefan Klein Member ✭
    Answer βœ“
    Options

    I found a solution, maybe it's of interest to someone.

    First I give every item in the array a unique id (random number). Makes it easier to target it later. Then I create an array outside of the main loop. Inside the main loop, I use the Find All Elements function to find the matched values. To prevent that one of these items get processed again later in the loop, I push the token of it into the assigned array to keep track of them.

    All I need to do now is to check at the start of the main loop with an if-condition, if the current item token is already in the array, hence already processed. In hindsight, it seems so easy and obvious to me. I was so fixated on removing the found items, that it didn't come to me that I just have to keep track of which ones were already processed.

    Cost me a lot of time, but maybe I can save some time for somebody else. Keep building πŸ’ͺ🏼

Answers

  • Liz Anaya
    Liz Anaya Member, Administrator

    ADMIN

    Options

    Hi @Stefan Klein !

    Have you tried the remove array filter?

    remove filter
    Remove any elements from the array that match the supplied value and return the new array.

    Here is an example of how it works:

    Given the array below, we want to remove any objects with the value "Ford" in the key "name."
    [ { "name":"Ford"}, { "name":"GM" }, { "name":"Chevrolet" } ]


    In this example, we will need to specify the value and path.

    After we apply the remove filter, the array becomes: [ { "name":"GM" }, { "name":"Chevrolet" } ]

    Let me know if this is helpful or if you have any additional questions. 😊

  • Stefan Klein
    Stefan Klein Member ✭
    Options

    Hi @lizanaya

    Thanks, but that doesn't work πŸ™ I tried to loop through the second array and remove matching items from the first array. It did not remove them, but it empties my "publisher_aggregated" array. Not sure what I'm doing wrong.

    I'm also wondering why the approach with diff_assoc doesn't give me the desired result. Shouldn't it return only the elements in the array, that are not in the second array?

  • Liz Anaya
    Liz Anaya Member, Administrator

    ADMIN

    Options

    Hi @Stefan Klein!

    I think the for each loop might be causing the issue here. Check out this Loom video that walks through how to use both the Find All Elements array function and the remove filter. Let me know if it's helpful!

  • Stefan Klein
    Stefan Klein Member ✭
    Options

    Hi @lizanaya

    Thank you so much for taking the time and creating this video. Very appreciated. The problem is, I need to find all elements not only one time but multiple times. That's because every publisher has multiple entries per quarter which I need to cumulate till all items are processed. Hence the loop. After every iteration, I need to remove the processed item from the original array I'm looping through. Otherwise, this item will be found again when there's another matching item and then I'll have it processed again.

    Any idea how I could approach that? I tried so many things already but failed every time.

  • Liz Anaya
    Liz Anaya Member, Administrator

    ADMIN

    Options

    @Stefan Klein So happy to hear you found a solution! πŸ€— πŸŽ‰ Thank you so much for sharing it with the community. It's super valuable in case someone comes across a similar issue.

    I believe I already mentioned this, but if you are ever interested, we host free office hours on Tuesdays where the Xano team answers questions live. You can RSVP here.