Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
Is there a way to match the _id of one mongodb array to the corresponding clientId from another array?

I have 2 arrays coming from my MongoDB database, one called users that returns a list of user objects like this:

[{
email: "poe@email.com",
firstName: "Poe",
lastName: "Damaren",
_id: "bb8"
}]

and one called treatments that has a corresponding clientId that should match the _id from one of the objects in the user array, ie:

[{
clientId: "bb8",
date: "2022-02-04",
treatment "fixed antenna",
_id: "r2d2"
}]

On my client side using React, I'm trying to show a table with a row for every treatments object where the date is greater than today, and then show a column for the date, and a column for the firstName and lastName from the user object whose _id corresponds to the clientId from the treatment object (I hope that makes sense). I've tried putting it together, but I get stuck trying to show the first and last name. This is where I'm at:

<table>
   <thead>
      <tr>
        <th>Date</th>
        <th>Name</th>
      </tr>
    </thead>
    <tbody>
      {treatments?.map((t)=>(
         new Date(t?.date) >= today ? (                  
            <tr>
              <td>{t?.date}</td>
              <td>{users?.firstName?.find(u=>u?._id === t?.clientId)}</td>
            </tr>
          ) : (
            <div></div>
          ))
        )}
    </tbody>
</table>

Is there a way to get the first name and last name to show within a table row for each element in the treatments array? Something like this:

Date Name
2022-02-04 Poe Damaren

Help me stackoverflow community, you're my only hope...

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can write a simple function to retrieve the full name.

const getFullName = t => {
    const { firstName = "", lastName = "" } = users?.find(
        u => u?._id === t?.clientId
    );
    return `${firstName} ${lastName}`;
};
<tr>
    <td>{t?.date}</td>
    <td>{getFullName(t)}</td>
</tr>

Edit beautiful-matsumoto-89rb3

NOTE: I would attach the firstName and lastName to each treatment on the backend side to reduce computations done on the UI side. Same for filtering treatments using the date.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda