I have the following code:

const fetchUsers = async () => {

const users = [
   { id: 27, email: '[email protected]' },
  { id: 12754, email: '[email protected]' },
  { id: 12158, email: '[email protected]' },
  { id: 698115737, email: '[email protected]' },

]

const userAccessTable = 
  [
  {
    id: 12754,
    WorkEmail: '[email protected]',
    ClubNumber: '7908'
  },
  {
    id: 12158,
    WorkEmail: '[email protected]',
    ClubNumber: '7729'
  },
  {
    id: 4040,
    WorkEmail: '[email protected]',
    ClubNumber: '6437'
  },
  {
    id: 4040,
    WorkEmail: '[email protected]',
    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: '[email protected]'},
    { userId: 12158, club: '7729', email: '[email protected]' },
    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: '[email protected]' },
  { id: 12754, email: '[email protected]' },
  { id: 12158, email: '[email protected]' },
  { id: 698115737, email: '[email protected]' },

]

const userAccessTable = 
  [
  {
    id: 12754,
    WorkEmail: '[email protected]',
    ClubNumber: '7908'
  },
  {
    id: 12158,
    WorkEmail: '[email protected]',
    ClubNumber: '7729'
  },
  {
    id: 4040,
    WorkEmail: '[email protected]',
    ClubNumber: '6437'
  },
  {
    id: 4040,
    WorkEmail: '[email protected]',
    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: '[email protected]' },
  { id: 12754, email: '[email protected]' },
  { id: 12158, email: '[email protected]' },
  { id: 698115737, email: '[email protected]' },

]

const userAccessTable = 
  [
  {
    id: 12754,
    WorkEmail: '[email protected]',
    ClubNumber: '7908'
  },
  {
    id: 12158,
    WorkEmail: '[email protected]',
    ClubNumber: '7729'
  },
  {
    id: 4040,
    WorkEmail: '[email protected]',
    ClubNumber: '6437'
  },
  {
    id: 4040,
    WorkEmail: '[email protected]',
    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: '[email protected]' },
      { id: 12754, email: '[email protected]' },
      { id: 12158, email: '[email protected]' },
      { id: 698115737, email: '[email protected]' },

    ]

    const userAccessTable = 
      [
      {
        id: 12754,
        WorkEmail: '[email protected]',
        ClubNumber: '7908'
      },
      {
        id: 12158,
        WorkEmail: '[email protected]',
        ClubNumber: '7729'
      },
      {
        id: 4040,
        WorkEmail: '[email protected]',
        ClubNumber: '6437'
      },
      {
        id: 4040,
        WorkEmail: '[email protected]',
        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)

Write a comment

Your email address will not be published. Required fields are marked *