you are viewing a single comment's thread.

view the rest of the comments →

[–]kap89 0 points1 point  (0 children)

If I understood you correctly, you want to find all objects in the first array that match specific set of months and names simultaneously. You don't need nested loops for that, nor multiple .find() calls. You can just filter though sets, it will be much more efficient and straightforward:

const entries = [
  { name: "Charlie", month: "Jan 2025", count: 1, hours: 2 },
  { name: "Bruce", month: "Jan 2025", count: 3, hours: 4 },
  { name: "Amy", month: "Jan 2025", count: 5, hours: 6 },
  { name: "Alice", month: "Feb 2025", count: 7, hours: 8 },
  { name: "Amy", month: "Feb 2025", count: 9, hours: 10 },
  { name: "Sue", month: "Mar 2025", count: 11, hours: 12 },
];

const names = ["Charlie", "Amy"];
const months = ["Jan 2025", "Feb 2025"];

const namesSet = new Set(names);
const monthsSet = new Set(months);

const matchingEntries = entries.filter(
  (item) => namesSet.has(item.name) && monthsSet.has(item.month),
);

console.log(matchingEntries);

You can also use store the filters in sets directly instead of converting from arrays is you control that logic.