我有两个这样的数组:
owners: [
{
userID: "58c4d7ac",
username: "John.Doe",
firstName: "John",
lastName: "Doe",
email: "John.Doe@acme.com"
},
{
userID: "68c4d7ac",
username: "User2.Name2",
firstName: "User2",
lastName: "Name2",
email: "dh@acme.com"
}
]
users: [
{
userID: "58c4d7ac",
username: "John.Doe",
firstName: "John",
lastName: "Doe",
email: "John.Doe@acme.com"
},
{
userID: "68c4d7ac",
username: "User2.Name2",
firstName: "User2",
lastName: "Name2",
email: "dh@acme.com"
},
{
userID: "88c4d7ac",
username: "User3.Name3",
firstName: "User3",
lastName: "Name3",
email: "dh@acme.com"
}
]
我想得到一个用户数组,其中只包含不在owners数组中的元素。
我尝试了不同的方法。最后,我得出了解决方案:
const usersItems = users.map(user => {
// Check whether the user is already an owner
if (owners.findIndex(owner => owner.userID === user.userID) === -1) {
return owner
} else {
return null;
}
});
console.log(usersItems);
// Filter out all items which are null
const newUsersItems = usersItems.filter(user => {
if (user) return user;
});
console.log(usersItems);
在我看来,这似乎不是一个干净的解决方案。有没有更清洁,更轻松的方法来做到这一点?结果,我希望拥有:
newUsers: [
{
userID: "88c4d7ac",
username: "User3.Name3",
firstName: "User3",
lastName: "Name3",
email: "dh@acme.com"
}
]
答案 0 :(得分:6)
您可以摆脱map
,而只需使用过滤器(这正是过滤器的目的)
const filtered = users.filter(user => {
// Check whether the user is already an owner
return owners.findIndex(owner => owner.userID === user.userID) === -1
});
可能会起作用
答案 1 :(得分:2)
首先,您可以使用userID's
数组的owners
创建 Set() ,然后可以使用 Array.filter() < / {strong}(在users
数组上)以过滤userID
不属于先前创建的集合的用户。
const owners = [
{userID: "58c4d7ac", username: "John.Doe", firstName: "John", lastName: "Doe", email: "John.Doe@acme.com"},
{userID: "68c4d7ac", username: "User2.Name2", firstName: "User2", lastName: "Name2", email: "dh@acme.com"}
];
const users = [
{userID: "58c4d7ac", username: "John.Doe", firstName: "John", lastName: "Doe", email: "John.Doe@acme.com"},
{userID: "68c4d7ac", username: "User2.Name2", firstName: "User2", lastName: "Name2", email: "dh@acme.com"},
{userID: "88c4d7ac", username: "User3.Name3", firstName: "User3", lastName: "Name3", email: "dh@acme.com"}
];
let ownerIdsSet = new Set(owners.map(x => x.userID));
let res = users.filter(x => !ownerIdsSet.has(x.userID));
console.log(res);
.as-console {background-color:black !important; color:lime;}
但是为什么要先构建一个Set?
总而言之,它将提高过滤过程的性能,尤其是在owners
数组较大的情况下。您应注意,findIndex()
,find()
和some()
之类的方法需要遍历数组以检查相关条件,同时检查userID
是否属于{{1} }是 Set
计算。但是,当然,在创建上述Set时,初始化时会产生额外的负担。
答案 2 :(得分:2)
const owners = [
{
userID: "58c4d7ac",
username: "John.Doe",
firstName: "John",
lastName: "Doe",
email: "John.Doe@acme.com"
},
{
userID: "68c4d7ac",
username: "User2.Name2",
firstName: "User2",
lastName: "Name2",
email: "dh@acme.com"
}
];
const users = [
{
userID: "58c4d7ac",
username: "John.Doe",
firstName: "John",
lastName: "Doe",
email: "John.Doe@acme.com"
},
{
userID: "68c4d7ac",
username: "User2.Name2",
firstName: "User2",
lastName: "Name2",
email: "dh@acme.com"
},
{
userID: "88c4d7ac",
username: "User3.Name3",
firstName: "User3",
lastName: "Name3",
email: "dh@acme.com"
}
];
const result = users.filter(user => !owners.some(owner => owner.userID === user.userID));
console.log(result);
答案 3 :(得分:1)
您可以使用过滤器和一些
const owners = [{userID:"58c4d7ac",username:"John.Doe",firstName:"John",lastName:"Doe",email:"John.Doe@acme.com"},{userID:"68c4d7ac",username:"User2.Name2",firstName:"User2",lastName:"Name2",email:"dh@acme.com"}]
const users = [{userID:"58c4d7ac",username:"John.Doe",firstName:"John",lastName:"Doe",email:"John.Doe@acme.com"},{userID:"68c4d7ac",username:"User2.Name2",firstName:"User2",lastName:"Name2",email:"dh@acme.com"},{userID:"88c4d7ac",username:"User3.Name3",firstName:"User3",lastName:"Name3",email:"dh@acme.com"}]
const newUsers = users
.filter(({userID}) => !owners.some(({userID:ownerID})=> ownerID === userID))
console.log(newUsers)
答案 4 :(得分:1)
您可以仅使用单个.filter()
函数,如下所示:
let owners = [{userID: "58c4d7ac",username: "John.Doe",firstName: "John",lastName: "Doe",email: "John.Doe@acme.com"},{userID: "68c4d7ac",username: "User2.Name2",firstName: "User2",lastName: "Name2",email: "dh@acme.com"}];
let users = [{userID: "58c4d7ac",username: "John.Doe",firstName: "John",lastName: "Doe",email: "John.Doe@acme.com"},{userID: "68c4d7ac",username: "User2.Name2",firstName: "User2",lastName: "Name2",email: "dh@acme.com"},{userID: "88c4d7ac",username: "User3.Name3",firstName: "User3",lastName: "Name3",email: "dh@acme.com"}];
let newUsersItems = users.filter(user => owners.findIndex(owner => owner.userID === user.userID) === -1);
console.log(newUsersItems)
您可以仅使用单个.filter()
函数,如下所示:
答案 5 :(得分:0)
您可以使用reduce函数
const diff = users.reduce((acc, user) => {
if(!owners.find(owner => owner.id === user.id){
acc.push(user);
}
return acc;
}, []);
为避免在每个循环中使用find()函数,您可以将所有者ID与地图一起存储在数组中,而只需使用include()
const ownersIds = owners.map(owner => owner.id);
const diff = users.reduce((acc, user) => {
if(!ownersIds.includes(user.id){
acc.push(user);
}
return acc;
}, []);
答案 6 :(得分:0)
您可以使用函数some
或find
,这种方法使用函数find
let owners = [ { userID: "58c4d7ac", username: "John.Doe", firstName: "John", lastName: "Doe", email: "John.Doe@acme.com" }, { userID: "68c4d7ac", username: "User2.Name2", firstName: "User2", lastName: "Name2", email: "dh@acme.com" } ],
users = [ { userID: "58c4d7ac", username: "John.Doe", firstName: "John", lastName: "Doe", email: "John.Doe@acme.com" }, { userID: "68c4d7ac", username: "User2.Name2", firstName: "User2", lastName: "Name2", email: "dh@acme.com" }, { userID: "88c4d7ac", username: "User3.Name3", firstName: "User3", lastName: "Name3", email: "dh@acme.com" } ],
result = users.filter(({userID}) => !owners.find(o => o.userID === userID));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }