【发布时间】:2022-01-11 09:45:44
【问题描述】:
首先,我知道关于这个问题有很多答案,但我对他们有一些问题,这就是为什么我发布关于这个主题的另一个问题。
这是我的对象数组:
0: {id: 'lAYOUT', label: 'lAYOUT', items: 'val1'}
1: {id: 'tecst', label: 'tecst', items: 'val1'}
2: {id: 'tecst', label: 'tecst', items: 'val1'}
我试图过滤掉只有 2 个值,因为数组中有 2 个相同的对象。我想通过items 和label 制作独特的对象。
这就是我尝试使用lodash 的方式:
const filteredArray = uniq(nestedItems, (item, key, a) => item.items && item.label)
但它仍然会返回所有 3 个元素。
我也试过这样:
const filteredArray = [...new Set(nestedItems)]
【问题讨论】:
-
数组中的
key是什么?? -
@MayurVaghasiya 我的错,我复制/粘贴错误。而不是
key,应该是label,但问题存在,我只是错误地复制了它。
标签: javascript arrays unique