Prevent .map from returning undefined for values that don’t meet a certain criteria
I have the following code:
const fetchUsers = async () => { const users = [ { id: 27, email: 'support@domo.com' }, { id: 12754, email: 'gilberthigley@email.com' }, { id: 12158, email: 'springcreek@email.com' }, { id: 698115737, email: 'atroxler@email.com' }, ] const userAccessTable = [ { id: 12754, WorkEmail: 'gilberthigley@email.com', ClubNumber: '7908' }, { id: 12158, WorkEmail: 'springcreek@email.com', ClubNumber: '7729' }, { id: 4040, WorkEmail: 'dduschen@email.com', ClubNumber: '6437' }, { id: 4040, WorkEmail: 'dduschen@email.com', ClubNumber: '6438' }, ] const userAccess = await Promise.all( users.map(user => { let res userAccessTable.map(data => { if (data.WorkEmail.toLowerCase().includes(user.email)) { return res = { userId: user.id, club: data.ClubNumber, email: data.WorkEmail } } }) return res }) ) console.log(userAccess) } fetchUsers()
I am trying to cross-reference the users with the userAccessTable and if they have the same email set the object to a variable, but because I am currently doing so while mapping I get the following output:
[ undefined, { userId: 12754, club: '7908', email: 'gilberthigley@email.com'}, { userId: 12158, club: '7729', email: 'springcreek@email.com' }, undefined ]
is there a way to prevent the undefined values from coming back for the results that don’t have a similar email? here is a repl.it link https://repl.it/join/pvnetqkc-rterrell25
Answer
Ciao, you could initialize res
to an empty object like this:
const users = [ { id: 27, email: 'support@domo.com' }, { id: 12754, email: 'gilberthigley@email.com' }, { id: 12158, email: 'springcreek@email.com' }, { id: 698115737, email: 'atroxler@email.com' }, ] const userAccessTable = [ { id: 12754, WorkEmail: 'gilberthigley@email.com', ClubNumber: '7908' }, { id: 12158, WorkEmail: 'springcreek@email.com', ClubNumber: '7729' }, { id: 4040, WorkEmail: 'dduschen@email.com', ClubNumber: '6437' }, { id: 4040, WorkEmail: 'dduschen@email.com', ClubNumber: '6438' }, ] let result = users.map(user => { let res = {} userAccessTable.map(data => { if (data.WorkEmail.toLowerCase().includes(user.email)) { return res = { userId: user.id, club: data.ClubNumber, email: data.WorkEmail } } }) return res }) console.log(result)
Or you could filter
your final result like this:
const users = [ { id: 27, email: 'support@domo.com' }, { id: 12754, email: 'gilberthigley@email.com' }, { id: 12158, email: 'springcreek@email.com' }, { id: 698115737, email: 'atroxler@email.com' }, ] const userAccessTable = [ { id: 12754, WorkEmail: 'gilberthigley@email.com', ClubNumber: '7908' }, { id: 12158, WorkEmail: 'springcreek@email.com', ClubNumber: '7729' }, { id: 4040, WorkEmail: 'dduschen@email.com', ClubNumber: '6437' }, { id: 4040, WorkEmail: 'dduschen@email.com', ClubNumber: '6438' }, ] let result = users.map(user => { let res userAccessTable.map(data => { if (data.WorkEmail.toLowerCase().includes(user.email)) { return res = { userId: user.id, club: data.ClubNumber, email: data.WorkEmail } } }) return res }) console.log(result.filter(el => el !== undefined))
EDIT
To avoid empty object on result you could use forEach
instead of map
like:
const users = [ { id: 27, email: 'support@domo.com' }, { id: 12754, email: 'gilberthigley@email.com' }, { id: 12158, email: 'springcreek@email.com' }, { id: 698115737, email: 'atroxler@email.com' }, ] const userAccessTable = [ { id: 12754, WorkEmail: 'gilberthigley@email.com', ClubNumber: '7908' }, { id: 12158, WorkEmail: 'springcreek@email.com', ClubNumber: '7729' }, { id: 4040, WorkEmail: 'dduschen@email.com', ClubNumber: '6437' }, { id: 4040, WorkEmail: 'dduschen@email.com', ClubNumber: '6438' }, ] let result = []; users.forEach(user => { let res = {} userAccessTable.forEach(data => { if (data.WorkEmail.toLowerCase().includes(user.email)) { res = { userId: user.id, club: data.ClubNumber, email: data.WorkEmail } result.push(res) } }) }) console.log(result)