当我提取一个对象字段时,为什么 lodash 的 _.unique 不返回唯一对象?

2024-01-11

我正在使用lodash的_.unique http://lodash.com/docs#uniq并且它没有按预期工作。我正在这样做:

uniqueByFocusIndex = _.unique(clickables, false, "focusIndex");

正如您在图像中看到的(看右边),它返回两个具有相同值的元素focusIndexes.我希望这会返回两者之一,而不是两者。是不是因为_.unique只适用于基元而不适用于对象?

Click to expand: enter image description here


_.uniqWith https://lodash.com/docs#uniqWith是您可能需要的,以便您可以使用进行比较_.isEqual https://lodash.com/docs#isEqual

_.uniqWith(clickables, _.isEqual)

建议在docs https://lodash.com/docs#uniqWith

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当我提取一个对象字段时,为什么 lodash 的 _.unique 不返回唯一对象? 的相关文章

随机推荐