• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

133
Vistas
How to join two array conditionally based on the date in javascript

I have two arrays of users which have the same iDs. I want to merge them based on the updatedAt property that each user has. The user who has newer updatedAt property has priority.

const users1 = [
    { id: 1, name: 'user1', childUsers: [2], updatedAt: '2022-02-23T00:00:00.000Z' },
    { id: 2, name: 'user2', childUsers: [3, 4], updatedAt: '2022-01-26T00:00:00.000Z' },
    { id: 3, name: 'user3', childUsers: [2, 3], updatedAt: '2022-02-24T00:00:00.000Z' },
    { id: 4, name: 'user4', childUsers: [2, 4], updatedAt: '2022-02-26T00:00:00.000Z' },
  ]
const users2 = [
    { id: 1, name: 'user1', childUsers: [2], updatedAt: '2022-02-26T00:00:00.000Z' },
    { id: 2, name: 'user2.1', childUsers: [3, 4], updatedAt: '2022-02-27T00:00:00.000Z' },
    { id: 3, name: 'user3', childUsers: [2, 3], updatedAt: '2022-02-26T00:00:00.000Z' },
    { id: 4, name: 'user4.1', childUsers: [2, 4], updatedAt: '2022-02-27T00:00:00.000Z' },
  ]

Output should be

const mergedUsers = [
{ id: 1, name: 'user1', childUsers: [2], updatedAt: '2022-02-26T00:00:00.000Z' },
{ id: 2, name: 'user2.1', childUsers: [3, 4], updatedAt: '2022-02-27T00:00:00.000Z' },
{ id: 3, name: 'user3', childUsers: [2, 3], updatedAt: '2022-02-26T00:00:00.000Z' },
{ id: 4, name: 'user4.1', childUsers: [2, 4], updatedAt: '2022-02-27T00:00:00.000Z' },

]

almost 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use Array.reduce() to group users by id, replacing any entry with a newer updatedAt property.

Once we have the grouped users, we can use Object.values() to get the desired result:

const users1 = [
    { id: 1, name: 'user1', childUsers: [2], updatedAt: '2022-02-23T00:00:00.000Z' },
    { id: 2, name: 'user2', childUsers: [3, 4], updatedAt: '2022-01-26T00:00:00.000Z' },
    { id: 3, name: 'user3', childUsers: [2, 3], updatedAt: '2022-02-24T00:00:00.000Z' },
    { id: 4, name: 'user4', childUsers: [2, 4], updatedAt: '2022-02-26T00:00:00.000Z' },
  ]

const users2 = [
    { id: 1, name: 'user1', childUsers: [2], updatedAt: '2022-02-26T00:00:00.000Z' },
    { id: 2, name: 'user2.1', childUsers: [3, 4], updatedAt: '2022-02-27T00:00:00.000Z' },
    { id: 3, name: 'user3', childUsers: [2, 3], updatedAt: '2022-02-26T00:00:00.000Z' },
    { id: 4, name: 'user4.1', childUsers: [2, 4], updatedAt: '2022-02-27T00:00:00.000Z' },
  ]

const result = Object.values([...users1, ...users2].reduce((acc, user) => { 
    // Entry either does not exist or has an older updatedAt property
    if (!acc[user.id] || (user.updatedAt > acc[user.id].updatedAt)) {
        acc[user.id] = user;
    }
    return acc;
}, {}))

console.log('Result:', result)

almost 3 years ago · Juan Pablo Isaza Denunciar

0

You want to compare each user in the first array with its similar one from the second array and choose the one with the higher updatedAt date.

const users1 = [...];
const users2 = [...];

const merged = users1.map((user1) => {
  // loop through the users1 array 
  const duplicateUser = users2.find((user2) => user1.id === user2.id);
  // if a similar user was found
  if (duplicateUser) {
    // compare and return the user with the newer updatedAt date
    return new Date(duplicateUser.updatedAt) > new Date(user1.updatedAt)
      ? duplicateUser
      : user1;
  }
  return user1; // in the case of no duplicates 
});

console.log(merged);

almost 3 years ago · Juan Pablo Isaza 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda