[英]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 个对象数组
itemsA
和itemsB
的最佳方法是什么。 The data once merged should be in mergedList
.合并后的数据应该在
mergedList
。
Criteria:标准:
source='STAPLE'
should not repeat in merged array.source='STAPLE'
不应在合并数组中重复。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;
}
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.