Get the first duplicate item from array and delete it

Options
lukas baier
lukas baier Member
edited September 2023 in ? Working with APIs

Hi,

I am struggling with the following request - I have an array of users which contains duplicates (which is desirable). When I call a specific API endpoint, I need to remove one of the duplicates (if duplicates exist, if not then delete the unique record) but keep the others.

Example:
- Existing array [1, 2, 2, 3, 2, 3]
- I want to remove the very first user 2
- Desired result [1, 2, 3, 2, 3] → the first user 2 removed, the other users nr. 2 kept

Standard Remove function always deletes all results with nr. 2 (I also tried to use the filter First but didn't help)

Thanks a lot!

Lukas

Answers

  • arturosanz
    arturosanz Member ✭✭
    edited September 2023
    Options

    I understand the unique array filter doesn't fit your case, so I guess the way to approach this is by looping through all array items like so…

    As you can see, the result is what you expect.

    Now the function details…

  • lukas baier
    Options

    @arturosanz thanks a lot for great first version! It works, however, partially for my use case. I just recorded a short Loom video where you can see my use case in detail :) https://www.loom.com/share/7d5d123c37454b0b9a94dae7c0bc391e?sid=3874c781-7dba-4805-8a93-0b7022d5496f

    Could you please help me to sort this out?

  • arturosanz
    arturosanz Member ✭✭
    edited September 2023
    Options

    @lukas baier the first requirement can easily be resolved by sorting the array before entering the loop. you just need to apply the sort filter to the array before starting to loop through it.

    Regarding the second requirement, you can handle it with a simple addition to the if stetement as long as you configure the number input to choose which one you want to delete like so…

    I am assuming that you are using the array elements as Xano's table ids, so they should be natural numbers starting from 1. Configuring the input this way, you have three possibilities:

    1. valid number (1 … n) → deletes only this number as long as it is repeated.
    2. 0 → deletes all repeated numbers.
    3. null → doesn't delete anything, leaving the array as it was but sorted.

    This is the modification you have to do to the if statement…

    IMPORTANT: Pay attention to the === operators. They are necessary for this to work.

    Here is an example…

    Original array:

    Results depending on the number input value:

    If you don't want to use the null option, just configure the input as non nullable and set the default value to 0, and the === operators won't be necessary either.

  • lukas baier
    Options

    HI @arturosanz so we made a progress, thanks for your support!

    However, 2 issues occured:
    a) if there is more than 1 duplicate (meaning like one record + 3 duplicates = 4 records with the same id → 1,1,1,1), it deletes all besides the one original record. So from 1,1,1,1 → 1….desired result would be: 1,1,1
    b) if there is only single record (no duplicates), it doesn't do anything

    I recorded the Loom video again to show you what I mean https://www.loom.com/share/a9226608b15143d1be44336c985e4444

    One more time, really appreciate your support!

  • arturosanz
    arturosanz Member ✭✭
    Options

    @lukas baier it would be better is you post here all the different case scenarios you want to handle, so I can understand completely what you need to do. At this time I'm not sure if I understood correctly what you want to achieve. Just post a list of examples with original array → result array that covers all cases and I will refactor the code for you. Make sure the sample arrays contain enough elements to avoid misunderstandings from my side.

  • arturosanz
    arturosanz Member ✭✭
    Options

    @lukas baier ok, so as I understand, you have students who pursue goals, and for each goal the student can have from 0 to N teachers who help him achieve the goal. The student can add teachers from his goals one by one, and remove one by one or several at once.

    When I watched the videos I found a bit strange storing teachers in a list of user ids with no relation to their respective goal. I don't know if you have a good reason not to do so, but if you don't have one, then I would recommend you to change the adult_id[] table field which is just a list of integers (user ids), for a list of objects like {"user_id": int, "goal_id": int}. Doing so, when the student removes a teacher from a goal, you will delete the object matching both properties, "user_id" and "goal_id", instead of deleting a random "adult_id" from the array. This will simplify your code and make it stronger against potential inconsistencies. You will be able to use the For Each loop, which is much easier to understand than the For loop, because every object in the list will be different (teachers can't be added twice to the same goal). You can easily control that also when the student adds a new teacher. Remember that you shouldn't trust the frontend, so better double check potential inconsistencies in the backend too. Maybe you won't even need the goal_id[] table field either, because you will have the goals inside the adult_id[] table field.

    I've also seen that you have two parameters to differentiate removing just one teacher from removing several at once. My suggestion is to not use the first one and use only the last one (the list), and when there is only one teacher to remove, then the list will contain only one element. The reason is because your code will be simpler. You just use the For Each loop on the input list, and everything will work well when the list has several elements, or just one, or even when it is empty.

  • arturosanz
    arturosanz Member ✭✭
    edited September 2023
    Options

    There are two nice array filters to handle your case. To add teachers-goals you can use the filter intersect, and to delete them you can use the filter diff.

    I've tested both, and intersect works well, but diff have some trouble dealing with objects. I've reported the issue to Xano to be fixed. Those filters are very nice because allow you to avoid looping through all array values, and do the job in just one line of code. But anyway, you can achieve the same with loops and if-then sentences until Xano fixes the diff filter.

    This is the add teacher code…

    This is the remove teacher/s code… (this often fails because of the bug on diff filter)

    I will share the details in the next post.

  • arturosanz
    arturosanz Member ✭✭
    Options

    This is what the add teacher endpoint does… (I added 3 teacher-goals, one by one, and the screenshot is the last one added)

    This is what the remove teacher/s endpoint does… (I removed one teacher-goal)

    The bug appears when I try to remove several at once…

  • arturosanz
    arturosanz Member ✭✭
    Options

    Now the add teacher code in detail…

  • arturosanz
    arturosanz Member ✭✭
    Options

    Now the remove teacher/s code in detail…

  • arturosanz
    arturosanz Member ✭✭
    edited September 2023
    Options

    @lukas baier WARNING → make sure the path names you set in objects are TEXT not INPUT or VAR. Xano's predictive text gives preference to matching texts in this order INPUT → VAR → TEXT.

  • lukas baier
    Options

    @arturosanz I am sorry for my late reply - Unfortunately, it didn't work for me. Somehow it didn't remove the records correctly. However, I came with a workaround for my client so all good now. Anyway, really appreciate your time and effort! Thanks!