我脑子里一片空白。我有的是 2 个数组:
- 应用的所有用户的列表
- 此表单的所有用户的列表
我想要做的是返回数组 1 和 2 之间的差异。
这是我的应用程序所有用户的列表:
[{0:
{id: 1234, email: test@test.com}
},{1:
{id: 1235, email: michael@test.com}
}]
那么数组 2 将如下所示:
[{0:
{id: 1234, email: test@test.com}
}]
所以我想要一个能够返回的数组(数组 1 和 2 之间的差异):
[{0:
{id: 1235, email: michael@test.com}
}]
这是我生成数组 1 的方法:
this.allUsersList = [];
this.sharingProvider.getUsersListRef().on('child_added', userSn => {
if(userSn.key) {
this.allUsersList.push({
id: userSn.key,
email: userSn.val().email
});
}
});
这是我生成数组 2 的方法:
this.sharingList = [];
this.sharingProvider.getFormsUsersRef(this.formId).on('child_added', fuSnap => {
this.sharingProvider.getUsersListRef().child(fuSnap.key).on('value', userSn => {
const myUserId = this.sharingProvider.getMyUserId();
if(userSn.key !== myUserId) {
this.sharingList.push({
id: userSn.key,
email: userSn.val().email
});
}
});
});
感谢任何帮助
最佳答案
您可以使用some and Foreach ...
let array1 = [
{0:
{id: 1234, email: 'test@test.com'}
},
{1:
{id: 1235, email: 'michael@test.com'}
}
]
let array2 = [
{0:
{id: 1234, email: 'test@test.com'}
}
],array3 = [];
array1.forEach(function(e){
let conditional = array2.some(function(element,i){
return e[i] && element[i].id === e[i].id && element[i].email === e[i].email
});
if(!conditional) array3.push(e);
});
console.log(array3);
关于javascript - Firebase 和 javascript - 将项目从一个列表排除到另一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46910768/