题意:"比较 ECMA6 集合的相等性"
问题背景:
How do you compare two javascript sets? I tried using ==
and ===
but both return false.
"如何比较两个 JavaScript 集合?我尝试使用 == 和 ===,但两者都返回 false。"
a = new Set([1,2,3]);
b = new Set([1,3,2]);
a == b; //=> false
a === b; //=> false
These two sets are equivalent, because by definition, sets do not have order (at least not usually). I've looked at the documentation for Set on MDN and found nothing useful. Anyone know how to do this?
"这两个集合是等价的,因为根据定义,集合没有顺序(至少通常没有)。我查看了 MDN 上关于 Set 的文档,但没有找到有用的信息。有人知道该怎么做吗?"
问题解决:
Try this: 尝试这种方式
const eqSet = (xs, ys) =>
xs.size === ys.size &&
[...xs].every((x) => ys.has(x));
const ws = new Set([1, 2, 3]);
const xs = new Set([1, 3, 2]);
const ys = new Set([1, 2, 4]);
const zs = new Set([1, 2, 3, 4]);
console.log(eqSet(ws, xs)); // true
console.log(eqSet(ws, ys)); // false
console.log(eqSet(ws, zs)); // false
标签:Set,false,xs,comparing,new,equality,ys,ECMA6,const
From: https://blog.csdn.net/suiusoar/article/details/143450340