FabricioG
FabricioG

Reputation: 3320

Return an entire object where filter and includes matches

I currently have several objects like so:

// not correct syntax just showing what members is
       const members = "-Lz8YxIL-XJotORQ3Bk1": Object {
            "address": "14539 Fasdfadsfrd Ave",
            "bDay": "10/01/2004",
            "city": "Norwalk",
            "email": "[email protected]",
            "first": "Frank",
        }, ...

I also have an object with a few UIDs like so:

const attendanceGroup.members =  {
  "-MxQvetKWRGNVO6EWIko": "-Lz8YxHiwp8QZW3TqAFn",
  "-MxSvxI-D53qlXtp1nzT": "-Lz8YxHiwp8QZW3TqAFn",
}

I would like to get the object returned of each object where the uid matches. I'm doing it like so:

  const groupMembers = Object.keys(members).filter(item => {
    return Object.values(attendanceGroup.members).includes(item);
  });

But this only returns the keys of the matching uid. I would like the whole object. How can I accomplish this?

Upvotes: 0

Views: 39

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 159905

Object.entries will give you the key, value pairs:

const attendanceGroupMembers = new Set(Object.values(attendanceGroup.members));
const groupMembers = Object.entries(members)
  .filter(([key]) => attendanceGroupMembers.has(key))
  .map(([_, members]) => members);

Upvotes: 1

Related Questions