Filter array of objects by value from API Response in Transformer

Hi Team,

I am new to retool and trying to filter the API array response based on 2 parameters. Here is my sample API response.

[
{
"id": "1",
"customer_name": "ABC",
"flag1": "Standard",
"flag2": "NULL"
},
{
"id": "2",
"customer_name": "CBF",
"flag1": NULL,
"flag2": "custom"
},
{
"id": "3",
"customer_name": "XYZ",
"flag1": NULL,
"flag2": "Standard"
},
{
"id": "4",
"customer_name": "MNO",
"flag1": "Standard",
"flag2": "Standard"
},
{
"id": "5",
"customer_name": "DXY",
"flag1": NULL,
"flag2": NULL
}
]

I want to filter the data based on flag1 = "Standard" or flag2 = "Standard" and get the unique customer names met with the filter data. The expected output here for the above API response is

ABC
XYZ
MNO

Any help is much appreciated.

Hey @saravanakumar84! Looks like you're trying to filter an array of objects by a set of specific values in each object, and then extract one particular object field. Actually a pretty common use case in Retool!

The way I would do this is something like:

// Filter for objects containing "standard" for flag1 or flag2
const filteredArray = originalArray.filter(function(item) {
  return (item.flag1 === "Standard" || item.flag2 === "Standard")
});

// Filter for unique objects using Lodash
const uniqueObjects = _.uniqBy(filteredArray, 'customer_name');

// Extract just the customer name from those objects
const customerNames = uniqueObjects.map(item => item.customer_name);
// Returns an array

You can also combine the last two steps for simpler syntax:

const uniqueCustomerNames = _.uniqBy(filteredArray, 'customer_name').map(item => item.customer_name);

Hope this helps!

2 Likes

Thanks, Justin. Yup. It helps to solve my problem.