合并多个对象数组并根据可选属性删除重复项

发布于 2025-01-17 14:51:11 字数 736 浏览 3 评论 0原文

说如果有这样的数组:

const arr1 = [
   { "id": "1", "type": "sales" },
   { "id": "2", "type": "finance" }
]

const arr2 = [
   { "type": "sales" },
   { "id": "2", "type": "finance" }
]

const arr3 = [
   { "id": "1", "type": "sales" },
   { "type": "sales" },
   { "type": "finance" }
]

如下所示,id是可选的。我需要以这种方式合并数组,以使唯一性应基于id(如果存在),否则将基于整个对象的其余部分。 前任。在这里合并的数组将是:

[ 
  { "id": "1", "type": "sales" }, 
  { "type": "sales" }, 
  { "id": "2", "type": "finance" }, 
  { "type": "finance" } 
]

loadash具有.unionby,但可选的唯一性不起作用。

const result = _.unionBy(arr1, arr2, arr3, 'id')

可能我必须迭代每个人,但是我想知道是否有任何更简单的选择。

Say if there are arrays like:

const arr1 = [
   { "id": "1", "type": "sales" },
   { "id": "2", "type": "finance" }
]

const arr2 = [
   { "type": "sales" },
   { "id": "2", "type": "finance" }
]

const arr3 = [
   { "id": "1", "type": "sales" },
   { "type": "sales" },
   { "type": "finance" }
]

As you can see, id is optional. I need to merge arrays in such way that uniqueness should be based on id if present, else entire rest of the object.
ex. here merged array would be:

[ 
  { "id": "1", "type": "sales" }, 
  { "type": "sales" }, 
  { "id": "2", "type": "finance" }, 
  { "type": "finance" } 
]

loadash has .unionBy but optional uniqueness doesn't work.

const result = _.unionBy(arr1, arr2, arr3, 'id')

Probably I have to iterate through each, but I was wondering if there is any simpler alternative for this.

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

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

发布评论

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

评论(1

泪眸﹌ 2025-01-24 14:51:11

代替_。unionby您可以使用 _. unionwith 接受自定义比较器。 The logic for comparison is:

  1. Compare by ID if both items have an ID.
  2. 如果两个项目都不具有ID,则按类型进行比较。
  3. 如果一个人没有一个ID,则认为它们与众不同。
const arr1 = [
   { "id": "1", "type": "sales" },
   { "id": "2", "type": "finance" }
]

const arr2 = [
   { "type": "sales" },
   { "id": "2", "type": "finance" }
]

const arr3 = [
   { "id": "1", "type": "sales" },
   { "type": "sales" },
   { "type": "finance" }
]

const result = _.unionWith(arr1, arr2, arr3, (item1, item2) => {
  const item1HasId = _.has(item1, "id");
  const item2HasId = _.has(item2, "id");
  
  if (item1HasId && item2HasId) //if both have ID...
    return item1.id === item2.id; // ...compare by ID
    
  if (!item1HasId && !item2HasId) //if neither has ID...
    return item1.type === item2.type; // ...compare by type
    
  return false; // otherwise not equal
});

console.log(result);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Instead of _.unionBy you can use _.unionWith which accepts a custom comparator. The logic for comparison is:

  1. Compare by ID if both items have an ID.
  2. Compare by type if both items do not have an ID.
  3. Consider them different if one has an ID the other not.

const arr1 = [
   { "id": "1", "type": "sales" },
   { "id": "2", "type": "finance" }
]

const arr2 = [
   { "type": "sales" },
   { "id": "2", "type": "finance" }
]

const arr3 = [
   { "id": "1", "type": "sales" },
   { "type": "sales" },
   { "type": "finance" }
]

const result = _.unionWith(arr1, arr2, arr3, (item1, item2) => {
  const item1HasId = _.has(item1, "id");
  const item2HasId = _.has(item2, "id");
  
  if (item1HasId && item2HasId) //if both have ID...
    return item1.id === item2.id; // ...compare by ID
    
  if (!item1HasId && !item2HasId) //if neither has ID...
    return item1.type === item2.type; // ...compare by type
    
  return false; // otherwise not equal
});

console.log(result);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

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