Filter array based on substrings of another array

I have a use case where I have an array returned from AWS S3 but it produces too many results that are not needed for the user to see. I have an exclude list in a text box component that I would like to compare to the S3 array and have the js query filter out all values in the exclude list to get my final result set. So far I have not been able to get this to work, the js code I tried is below:

function removeSubstringElements(mainArray, subArray) {
return mainArray.filter((element) => {
return !subArray.some((substring) => {
return element.includes(substring);
});
});
}

// Example usage:
const mainArray = [test_test_bucket_checker.data];
const subArray = [text3.value];

const filteredArray = removeSubstringElements(mainArray, subArray);
return filteredArray;

It doesn't seem like this filters at all, when I run this I get all 7 results, when in fact I should get 5 results based on the exclude list(I only have 2 items in the exclude list for my testing). It's Can someone please help or have any ideas? Thanks

Have you tried using lodash _.pick()?
In Retool you can use _.pick()

1 Like

Hi @ScottR Thanks for your quick reply, I am fairly novice with javascript, so I am not sure how to approach that function. Is it possible you could provide an example?

No problem. Except I won’t be able to post until tomorrow morning. Maybe some one else will chime in before then. If not I will follow up. Apologies for the delay.

You could do the following in a js query

const mainArray = [test_test_bucket_checker.data];
const subArray = [text3.value];

return formatDataAsArray(_.pick(formatDataAsObject(mainArray),subArray))

Though if you post some sample data, if the above doesn't work, I will dig deeper...

Hi @ScottR Thank you for the prompt response, unfortunately this does not work either, seems like no matter what I try the key's i need don't filter to what is intended. It does work though if I put values in quotes like:
const mainArray = ["apple", "banana", "orange", "kiwi"];
const subArray = ["le", "an"];

Some sample data here:

In the mainArray I have the below:

In the subArray I have the below:
image

The outcome here would be to be left with the result key "Test/FID/Caremark/To_Accolade/FID_CM_RXCLM_20200304192335989731199123txt.gpg" based on taking out the values with a substring of CONFIG. This data is coming from S3 API. I am wondering if I need to convert to strings?

Note that in the following subArray, it contains values that are in the main array and those are what is picked:

const mainArray = [{'test':'a','test1':2,'test2':3}];
const subArray = ['test1','test2']
return formatDataAsArray(_.pick(formatDataAsObject(mainArray),subArray))

So, your subArray should contain keys that are in the mainArray...

Hi @ScottR So I will need to use the _.omit function then as I want the opposite behavior(I want the mainArray to contain keys that are not in the subArray), still though in my sample data even with _.pick I should still only get the keys with CONFIG in them, correct?

They keys used must match using _.pick and so I would assume for _.omit as well

@ScottR Thanks for your help, I will keep plugging away for now

1 Like

Hi @ScottR, since you have been helpful, do you know anything about this other post I made regarding a 404 error? Check If URL produces a 404 error