打字稿以简单的方式找到两个对象数组之间的区别
Typescript find difference between two array of objects in simple way
我有一个如下所示的数组对象
allRoles =
[
{ id: "0", name: "Tester", description: "Role is testing" },
{ id: "1", name: "Developer", description: "Role is Developer" },
{ id: "2", name: "Admin", description: "Role is Admin" },
{ id: "3", name: "Manager", description: "Role is Manager" },
{ id: "4", name: "Reviewer", description: "Role is Reviewer" }
]
assignedRoles =
[
{ id: "0", name: "Tester", description: "Role is testing" },
{ id: "4", name: "Reviewer", description: "Role is Reviewer" }
]
将 id 视为每个对象的唯一
我需要输出为
allowedRoles =
[
{ id: "1", name: "Developer", description: "Role is Developer" },
{ id: "2", name: "Admin", description: "Role is Admin" },
{ id: "3", name: "Manager", description: "Role is Manager" }
]
有很多答案都在使用 for 循环或比较器。
我想找出的是非常简单的解决方案,而不是编写复杂的代码。
幸运的是我能够弄清楚并找到非常简单的答案。
就一个班轮!!!
this.allowedRoles=this.allRoles.filter(role=> !this.assignedRoles.some(present=> present.id==role.id));
我有一个如下所示的数组对象
allRoles =
[
{ id: "0", name: "Tester", description: "Role is testing" },
{ id: "1", name: "Developer", description: "Role is Developer" },
{ id: "2", name: "Admin", description: "Role is Admin" },
{ id: "3", name: "Manager", description: "Role is Manager" },
{ id: "4", name: "Reviewer", description: "Role is Reviewer" }
]
assignedRoles =
[
{ id: "0", name: "Tester", description: "Role is testing" },
{ id: "4", name: "Reviewer", description: "Role is Reviewer" }
]
将 id 视为每个对象的唯一 我需要输出为
allowedRoles =
[
{ id: "1", name: "Developer", description: "Role is Developer" },
{ id: "2", name: "Admin", description: "Role is Admin" },
{ id: "3", name: "Manager", description: "Role is Manager" }
]
有很多答案都在使用 for 循环或比较器。 我想找出的是非常简单的解决方案,而不是编写复杂的代码。
幸运的是我能够弄清楚并找到非常简单的答案。 就一个班轮!!!
this.allowedRoles=this.allRoles.filter(role=> !this.assignedRoles.some(present=> present.id==role.id));