Find items in arrays
Problem:
Given a team id, find member's information.
const teams = [ { id: "t1", name: "Team 1", members: ["u1", "u2"] }, { id: "t2", name: "Team 2", members: ["u1", "u2", "u3"] },];
const users = [ { id: "u1", fullName: "Mars", role: "Engineer" }, { id: "u2", fullName: "Chester", role: "Consultant" }, { id: "u3", fullName: "Obi", role: "Frontend Developer" },];
const teamId = "t1";
Solution:
// find teamconst selectedTeam = teams.find((team) => team.id === teamId);
// find memebersconst members = selectedTeam.members;
const selectedMembers = [];for (const member of members) { const selectedUser = users.find((user) => user.id === member); selectedMembers.push(selectedUser);}