本文介绍了具有集合的 es6 唯一对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了这个使用 es6 创建唯一数组的示例
I came across this example for creating unique arrays with es6
[ ...new Set(array) ]
这似乎工作正常,直到我尝试使用一组对象并且它没有返回唯一的数组.
Which seems to work fine until I tried it with an array of objects and it didn't return unique array.
即
let item = [ ...new Set([{id:123,value:'test'},{id:123,value:'test'}]) ];
这是为什么?
推荐答案
根据文档
Set 对象允许您存储任何类型的唯一值,无论是原始值或对象引用.
现在 Set
构造函数中每个数组的引用将不同,因此构造函数不会将它们视为唯一值.
Now reference for each of those arrays inside that Set
constructor will be different so they are not considered to be a unique value by the constructor.
这篇关于具有集合的 es6 唯一对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!