es6具有set的唯一对象数组 [英] es6 unique array of objects with set

查看:180
本文介绍了es6具有set的唯一对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个用于创建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.

ie

let item = [ ...new Set([{id:123,value:'test'},{id:123,value:'test'}]) ];

为什么?

推荐答案


为什么?

Why is that ?

文档


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具有set的唯一对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆