如何根据javascript中的日期有条件地连接两个数组

发布于 2025-01-10 18:06:02 字数 1366 浏览 0 评论 0原文

我有两个具有相同 ID 的用户数组。我想根据每个用户拥有的 UpdatedAt 属性来合并它们。具有较新的updatedAt属性的用户具有优先权。

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

]

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

]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2025-01-17 18:06:02

您可以使用 Array.reduce( ) 按 id 对用户进行分组,用更新的 updatedAt 属性替换任何条目。

一旦我们对用户进行了分组,我们就可以使用 Object.values() 以获得所需的结果:

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)

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)

没有伤那来痛 2025-01-17 18:06:02

您想要将第一个数组中的每个用户与第二个数组中的相似用户进行比较,并选择 updateAt 日期较高的用户。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文