Home > Software design >  How to join two array conditionally based on the date in javascript
How to join two array conditionally based on the date in javascript

Time:03-01

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' },

]

CodePudding user response:

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)

CodePudding user response:

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);

  • Related