简体   繁体   English

如何使用 ES6 从对象数组中删除重复项?

[英]How to Remove duplicates from an array of objects using ES6?

I'm wondering what the best method would be to merge 2 array of objects itemsA and itemsB .我想知道合并 2 个对象数组itemsAitemsB的最佳方法是什么。 The data once merged should be in mergedList .合并后的数据应该在mergedList

Criteria:标准:

  1. Items with source='STAPLE' should not repeat in merged array.带有source='STAPLE'不应在合并数组中重复。
  2. Items with any other source may repeat.具有任何其他来源的项目可能会重复。 For exampele an item name: 'Ball' by source: 'USER' may exist twice.例如,项目name: 'Ball'source: 'USER'可能存在两次。

itemsA has 6 items, itemsB has 7 items and mergedList should have 11 items itemsA有 6 个项目, itemsB有 7 个项目, mergedList应该有 11 个项目

let itemsA = [
{name: 'Milk', source: 'STAPLE'},
{name: 'Bread', source: 'AD'},
{name: 'Egg', source: 'STAPLE'},
{name: 'Ball', source: 'USER'},
{name: 'Pasta', source: 'STAPLE'},
{name: 'Coke', source: 'AD'}];
let itemsB = [
{name: 'Milk', source: 'USER'},
{name: 'Bread', source: 'AD'},
{name: 'Egg', source: 'STAPLE'},
{name: 'Ball', source: 'USER'},
{name: 'Mango', source: 'USER'},
{name: 'Pasta', source: 'STAPLE'},
{name: 'Coke', source: 'USER'}]

mergedList should equal to mergedList应该等于

let mergedList = [
{name: 'Milk', source: 'STAPLE'},
{name: 'Bread', source: 'AD'},
{name: 'Egg', source: 'STAPLE'},
{name: 'Ball', source: 'USER'},
{name: 'Pasta', source: 'STAPLE'},
{name: 'Coke', source: 'AD'}] 
{name: 'Milk', source: 'USER'},
{name: 'Bread', source: 'AD'},
{name: 'Ball', source: 'USER'},
{name: 'Mango', source: 'USER'},
{name: 'Coke', source: 'USER'}]   ];
function merge(itemsA, itemsB) {
  let merged = [];
  itemsA.concat(itemsB).reduce((stapleSet, obj) => (obj.source != "STAPLE") ?
  (merged.push(obj), stapleSet) : 
  (stapleSet.has(obj.name) || merged.push(obj), 
  stapleSet.add(obj.name), stapleSet), new Set());

  return merged;
}
  • get two arrays.得到两个数组。
  • create a merged array to push objects to.创建一个合并的数组来推送对象。
  • concatenate the two item arrays together.将两个 item 数组连接在一起。
  • reduce the concatenated array - if the object source is staple and not in stapleSet add the name of the object to stapleSet -- Set objects only allow one of each entry -- then push object to merged array.减少连接的数组 - 如果对象源是主食而不是在主食stapleSet ,则将对象的名称添加到stapleSet集 - 设置对象只允许每个条目中的一个 - 然后将对象推送到merged数组。 Otherwise push object to he merged array.否则将对象推送到merged数组。

  • return the merged array from the function.从函数返回合并后的数组。

 let itemsA = [ {name: 'Milk', source: 'STAPLE'}, {name: 'Bread', source: 'AD'}, {name: 'Egg', source: 'STAPLE'}, {name: 'Ball', source: 'USER'}, {name: 'Pasta', source: 'STAPLE'}, {name: 'Coke', source: 'AD'}]; let itemsB = [ {name: 'Milk', source: 'USER'}, {name: 'Bread', source: 'AD'}, {name: 'Egg', source: 'STAPLE'}, {name: 'Ball', source: 'USER'}, {name: 'Mango', source: 'USER'}, {name: 'Pasta', source: 'STAPLE'}, {name: 'Coke', source: 'USER'}]; function merge(itemsA, itemsB) { let merged = []; itemsA.concat(itemsB).reduce((stapleSet, obj) => (obj.source != "STAPLE") ? (merged.push(obj), stapleSet) : (stapleSet.has(obj.name) || merged.push(obj), stapleSet.add(obj.name), stapleSet), new Set()); return merged; } console.log( merge(itemsA, itemsB) );

Edit : If-Else formatting for OP编辑:OP的If-Else格式

function merge(itemsA, itemsB) {
  let merged = [];
  itemsA.concat(itemsB).reduce((stapleSet, obj) => {
    if (obj.source != "STAPLE") {
      merged.push(obj);
      return stapleSet;
    } else {
      if (stapleSet.has(obj.name)) {
        return stapleSet;
      } else {
        merged.push(obj);
        stapleSet.add(obj.name);
        return stapleSet;
      }
    }
  }, new Set());
  return merged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM