【发布时间】:2019-05-01 17:51:46
【问题描述】:
假设一个对象数组如下:
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},
]
【问题讨论】:
标签: javascript dictionary filter unique reduce