假设给定以下对象数组:
Assuming an array of objects as follows:
const listOfTags = [
{id: 1, label: "Hello", color: "red", sorting: 0},
{id: 2, label: "World", color: "green", sorting: 1},
{id: 3, label: "Hello", color: "blue", sorting: 4},
{id: 4, label: "Sunshine", color: "yellow", sorting: 5},
{id: 5, label: "Hello", color: "red", sorting: 6},
]
如果标签和颜色相同,就会出现重复条目。在这种情况下,id = 1和id = 5的对象是重复的。
我该如何筛选这个数组并删除重复项?
我知道一些解决方案,比如使用以下方法对一个键进行过滤:
const unique = [... new Set(listOfTags.map(tag => tag.label)]
但是多个键怎么办呢?
根据评论的要求,这里是所需的结果:
[
{id: 1, label: "Hello", color: "red", sorting: 0},
{id: 2, label: "World", color: "green", sorting: 1},
{id: 3, label: "Hello", color: "blue", sorting: 4},
{id: 4, label: "Sunshine", color: "yellow", sorting: 5},
]