Using unique filter on an array with multiple objects

Options

Hi.

I am creating a function stack that takes an array with objects that have around 20 elements each from an API call. The objective is to grab 2-3 elements and use that to create aggregates.

The data source is for transactions meaning many to many, one to many and many to one relationships exist.

One of the outputs I'm looking for lifts the unique customer IDs, then aggregates the amounts for each time they have transacted at a particular cashier and returns the array, so you have a list of customer IDs and total amounts spent at the selected cashier.

The problem is that each object is unique already because each transaction has a unique ID. However the source data has a flaw in that some transactions have been double posted (it's the same exact transaction, but with it's own transaction ID.

My stack follows this process

  • Call the API and stores it into a variable Source
  • Grabs the customer IDs from Source and uses the unique filter on them and saves them in the variable IDs
  • Runs a for each loop on IDs checking Source for all instances they appear and stores them in Customer Transactions, then adds them to a new array (LIST) that combines the ID with the transaction (so you have objects that have the ID and the IDs transactions)
  • Updates the variable LIST to only have the ID and the IDs' transaction values (adding a sum to this gives me the cleaned up list)

However, the problem is some of the objects are duplicates, leading to the wrong aggregation.


I've tried to use the unique filter on the array of objects (expecting it to return all objects because they are all unique from above) which fails to run. I've however been able to get it to run on a list of elements which will also be inaccurate because there can be two transactions of the same amount at different times. I only need to exclude transactions of the same amount that take place at the same time.

2 questions,

  1. Would I be correct to assume unique filter only works on a list of elements and not a list of objects?
  2. Is there any clever way to pull this off?

Best Answer

  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Answer ✓
    Options

    Thanks! The path is incorrect here. Your value is the whole array of objects, which means the path is just which key to look at inside the objects. So, reset that path value and replace it with just col_trans_date.

Answers

  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options

    Hi @enyarick. Unique does work on a list of objects — you want to make sure that you're specifying a path, as Xano needs to know which field in the object to judge uniqueness by. This is usually why this filter fails, is that either a path was not specified when it should have been, or the opposite.

  • enyarick
    enyarick Member
    Options

    Thanks for the response @Chris Coleman!

    I'm using unique on the list of objects but perhaps not correctly and getting this error.

    To get here

    • I called an array via API and stored it in a variable Data
    • Each object in the array has multiple fields.
    • I then created a variable Uniquedata and applied the unique filter to the Data variable where I put in the path from the Data variable Data.ID as the field to judge the uniqueness by.

    Then when I ran the stack it threw up this error.

    I think I may still be following the wrong process.

  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options

    Can you show me how you have the filter set up?

  • enyarick
    enyarick Member
    Options

    Here's how I've set it up.

  • enyarick
    enyarick Member
    Options

    Thanks so much @Chris Coleman. Making this change did the trick!