Tray Platform / Useful Scripts / Removing duplicate objects

Removing duplicate objects

  • On this page

A limitation of the List Helpers remove duplicates function is that it can only remove simple items from an array, and it can't remove objects based on a particular field in that object being unique.

For example you may have just fetched an array of message objects from Data Storage (you might have created this array by setting the data with the 'Append to list' operation and then used the 'Get value' function to call your message list), which you want to make unique by their ticket_url:

{
"value": [
{
"attachment_text": "Support: hello there! I need some help syncing records",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1537",
"ticket_text": "Ticket 1537 from ronburgundy@news.com"
},
{
"attachment_text": "Copied from original request - 401 unathorized",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1459",
"ticket_text": "Ticket 1459 from klaus.kinsky@kinsky.com"
},
{
"attachment_text": "Hi There, I can't access my stuff",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1536",
"ticket_text": "Ticket 1536 from gretchen.mol@mol-shopping.com"
},
{
"attachment_text": "Hi Team - we need to process multiple records",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1564",
"ticket_text": "Ticket 1564 from roger@ramjet.com"
},
{
"attachment_text": "Roger here, following up on my previous message",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1564",
"ticket_text": "Ticket 1564 from roger@ramjet.com"
},
{
"attachment_text": "Help, I'm a bit stuck",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1556",
"ticket_text": "Ticket 1556 from penelope@pitstop.com"
},
{
"attachment_text": "It all seems to be lost",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1536",
"ticket_text": "Ticket 1536 from gretchen.mol@mol-shopping.com"
},
{
"attachment_text": "Hi, I need to process lots of data",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1173",
"ticket_text": "Ticket 1173 from sophia@copolla.com"
},
{
"attachment_text": "Hello, we need to troubleshoot some problems",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1591",
"ticket_text": "Ticket 1591 from pat@pending.com"
}
]
}

To do this we can use the uniqBy method with the Lodash Helper.

The Lodash uniqBy method requires 2 parameters:

  1. The array
  2. The field which should be unique

So if we have the above array of messages being returned by the storage-2 connector in our workflow we can use the following setup of the Lodash Helper to remove objects which contain the same ticket_url:

This will then remove tickets 1564 and 1536 from the above list of messages.