Filter based on multiple unique values

I’m trying to determine the best approach to the following.

I have two arrays, as per the below:

const a = [
{
  Employee: "00001",
  Seq: "01",
  ReportDate: "2021-03-10T00:00:00.000",
  ReportName: "test report",
},
{
  Employee: "00002",
  Seq: "01",
  ReportDate: "2020-03-10T00:00:00.000",
  ReportName: "test report 2",
},
];


const b = [
{
  Employee: "00001",
  EmplName: "William Apple",
  ReportDate: "2021-03-10T00:00:00.000",
  ReportName: "test report",
  Posted: "Y",
  Seq: "01",
},
{
  Employee: "00003",
  EmplName: "John Smith",
  ReportDate: "2021-03-10T00:00:00.000",
  ReportName: "test report 3",
  Posted: "Y",
  Seq: "01",
},
];

I want to filter out anything in array ‘b’, that does not contain ALL 4 of the exact values in in one of the objects within Array ‘a’.

So, as per the examples above, I would be left with:

// from array 'b'
const c = [
{
  Employee: "00001",
  EmplName: "William Apple",
  ReportDate: "2021-03-10T00:00:00.000",
  ReportName: "test report",
  Posted: "Y",
  Seq: "01",
}
];

I have looked at various ways to do this. I cannot just check to see if the objects are equal, as array ‘a’ will only contain 4 value/keys, and array ‘b’ will have many more / a dynamic amount.

Would using the Filter method through a loop and multiple IF statements be the best approach?

Answer

I would separate out the test for an exact object match from the looping code like this:

const subObject = (a, b) => 
  Object .entries (a) .every (([k, v]) => b [k] == v)

const matches = (xs, ys) => 
  ys .filter (y => xs .some (x => subObject (x, y)))

const a = [{Employee: "00001", Seq: "01", ReportDate: "2021-03-10T00:00:00.000", ReportName: "test report"}, {Employee: "00002", Seq: "01", ReportDate: "2020-03-10T00:00:00.000", ReportName: "test report 2"}]
const b = [{Employee: "00001", EmplName: "William Apple", ReportDate: "2021-03-10T00:00:00.000", ReportName: "test report", Posted: "Y", Seq: "01"}, {Employee: "00003", EmplName: "John Smith", ReportDate: "2021-03-10T00:00:00.000", ReportName: "test report 3", Posted: "Y", Seq: "01"}]

console .log (matches (a, b))

subObject checks whether every property of a has the same value in b.

matches uses that to filter the second list based on whether the first list has some element which is a subobject of it.

Both turn out to be pretty simple functions. While we could inline subObject inside matches, I think it’s useful on its own, and makes for nicer code like this.

Leave a Reply

Your email address will not be published. Required fields are marked *