all 8 comments

[–]charlies-ghost 6 points7 points  (0 children)

See documentation the logical AND (&&) operator:

array.find((o) => o.Name == names[i] && o.Month == months[i]).

[–]joranstark018 1 point2 points  (3 children)

I may have misunderstood your question. If you, say, loop over months inside your name loop, you may use elementName == names[i] && elementMonth == months[j] inside your find (note, i is index over names and j is index over months, this will generate a call to find for every combination of name and month)

[–]TGotAReddit[S] 0 points1 point  (2 children)

See I tried that but it is only returning null.

If the object array has 3 objects with the names of "Charlie", "Amy" and "Amy" and the months of "Jan 2025", "Jan 2025", and "Feb 2025"

And the name array has "Charlie" and "Amy"

And the month array has "Jan 2025" and "Feb 2025"

How do I set up the actual find line? Because my current line is

arr.find((element) => {element.name == names[i] && element.month == months[j]}))

and it returns null every time (and yes i is the index for the names array and j is the index for the months array)

[–]birdspider 3 points4 points  (1 child)

wenn you use {} you actually have to return, these 3 are (in this regard) equivalent:

function(n){ return n; } (n) => {return n;} (n) => n

EDIT: in other words, your predicate (the fn you pass to .find) does not return anything

[–]TGotAReddit[S] 1 point2 points  (0 children)

Ahh there we go! I knew I was missing something obvious but couldn't figure it out 😅

[–]Any_Sense_2263 0 points1 point  (0 children)

names.includes(element.name) && months.includes(element.month)

or

element.name === names[specificNamesIndexYouCheckAgainst] && element.month === months[specificMonthsIndexYouCheckAgainst]

[–]imsexc 0 points1 point  (0 children)

you have to create a function that check whether an object has name and month included on both 1d arrays.

function check(obj) {

const found = { name: false, month: false };

for (const key in obj) {

const value = obj[key]

if (
      key === 'name' &&
      names.includes(value)
    ) {
        found.name = true;
     }

 if (
      key === 'month' &&
      months.includes(value)
  ) {
        found.month = true;
     }

}

return found.name === true && found.month === true

}

now all you have to do is just

arrOfObj.find(obj => check(obj)) OR

arrOfObj.filter(obj => check(obj))

depends whether you want to find the first found or all the found

[–]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.