简体   繁体   English

从JavaScript中的对象数组中删除重复项并基于布尔属性进行过滤

[英]Removing duplicates and filtering based on a boolean property from array of objects in JavaScript

I have two array of objects with these properties: 我有两个具有以下属性的对象数组:

const a = [{name: 'Anna',email: 'anna@mac.com',flag: false},
  {name: 'Kelly',email: 'kelly@mac.com',flag: true}];
const b = [{name: 'Rana',email: 'rana@mac.com',flag: true},
  {name: 'Anna',email: 'anna@mac.com',flag: true},
  {name: 'Hank',email: 'Hank@mac.com',flag: false}];

I want to remove all the duplicates containing the flag of false value but both duplicates have flag of false then I just want to keep one of them. 我想删除所有包含错误值标志的重复项,但是两个重复项都具有错误标志,那么我只想保留其中一个。 But if any object doesn't have any duplicates then I want to keep it regardless of it's flag property. 但是,如果任何对象没有任何重复项,那么无论它的flag属性如何,我都希望保留它。 I tried this to remove duplicates but couldn't filter by the flag property. 我试过此操作以删除重复项,但无法通过flag属性进行过滤。

let cs = a.concat(b);
   cs = cs.filter((c, index, self) => 
index === self.findIndex((t) => (
    t.email === c.email  
))

Expected output would be like this: 预期的输出将是这样的:

[{"name":"Anna","email":"anna@mac.com","flag":true},
{"name":"Kelly","email":"kelly@mac.com","flag":true}, 
{"name":"Rana","email":"rana@mac.com","flag":true},
{"name":"Hank","email":"Hank@mac.com","flag":false}]

See Map , Array.prototype.map() , and Array.prototype.filter() for more info. 有关更多信息,请参见MapArray.prototype.map()Array.prototype.filter()

 // A. const A = [{name: 'Anna',email: 'anna@mac.com',flag: false}, {name: 'Kelly',email: 'kelly@mac.com',flag: true}] // B. const B = [{name: 'Rana',email: 'rana@mac.com',flag: true}, {name: 'Anna',email: 'anna@mac.com',flag: true}, {name: 'Hank',email: 'Hank@mac.com',flag: false}] // Unique Users. const uniqueUsers = (...arrays) => [...new Map([].concat.apply([], arrays).map(user => [user.email.toLowerCase(), user])).values()] // Proof. const C = uniqueUsers(A, B) console.log(C) // Optional Filtration By Flag. const D = C.filter(user => user.flag) 

It seems that you already know that a doesn't contain duplicates and b doesn't contain duplicates. 似乎您已经知道a不包含重复项, b不包含重复项。 So you can use 所以你可以使用

const a = [{name: 'Anna',email: 'anna@mac.com',flag: false},
  {name: 'Kelly',email: 'kelly@mac.com',flag: true}];
const b = [{name: 'Rana',email: 'rana@mac.com',flag: true},
  {name: 'Anna',email: 'anna@mac.com',flag: true},
  {name: 'Hank',email: 'Hank@mac.com',flag: false}];

const map = new Map();
for (const o of a) {
  map.set(o.email, o);
}
for (const o of b) {
  if (!map.has(o.email) || !map.get(o.email).flag)
    map.set(o.email, o);
  // else keep the one from a
}
return Array.from(map.values());

Another approach using reduce : 另一种使用reduce方法:

 const a = [{name: 'Anna',email: 'anna@mac.com',flag: false}, {name: 'Kelly',email: 'kelly@mac.com',flag: true}]; const b = [{name: 'Rana',email: 'rana@mac.com',flag: true}, {name: 'Anna',email: 'anna@mac.com',flag: true}, {name: 'Hank',email: 'Hank@mac.com',flag: false}]; let resp = [].concat(a, b) .reduce((acc, { name, email, flag }) => { if(!flag || acc.some(x => x.name == name && x.email == email)) return acc; return acc.concat({name, email, flag}); }, []) console.log(resp); 

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

相关问题 按对象的属性过滤对象的JavaScript数组并删除重复项 - Filtering JavaScript array of objects by property of an object and removing duplicates 根据属性值删除对象数组中的重复项 - Removing Duplicates in Array of Objects based on Property Values 根据 88222995402888 属性值从 javascript 对象数组中删除重复项 - Remove duplicates from javascript objects array based on object property value 从对象数组中删除重复项 - Removing duplicates from an array of objects 从对象数组中删除重复项 - Removing Duplicates from Array of Objects 如何根据条件检查对象数组中的重复属性 javascript - How to check the duplicates property in array of objects based on condition javascript 根据属性名称使用Lodash或Javascript过滤对象数组 - Filtering array of Objects Using Lodash or Javascript based on property name 根据 object 属性的前 3 个字从对象数组中删除重复项 - Remove duplicates from an array of objects based on the first 3 words of object property 使用 javascript 根据属性从数组中删除对象 - Remove objects from array based on their property with javascript 从 JavaScript 中的一组对象中删除重复项但在删除重复项之前合并一个字段 - Remove duplicates from an array of objects in JavaScript But merge one field before removing the duplicates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM