如何在Javascript中检查集合是否包含数组?
我想检查一个集合是否包含一个数组.
I would like to check if a set contains an array.
var x = new Set();
x.add(2);
x.has(2); //returns true, OK
x.add([3,4]);
x.has([3,4]); //returns false
我知道:
[1,2] == [1,2]; //returns false
我可以使用循环或 JSON.stringify 比较两个数组.但是如果集合中有数组怎么处理呢?
I can compare two arrays using loop or JSON.stringify. However how to deal with it in case of arrays inside a set?
Set
本质上使用 ===
来比较存储在集合中的值.如果您想自己解决这个问题,您将不得不求助于循环或创建自己的设置数据结构.当您制定解决方案时,您可以使其依赖于 equals
方法,以便每个对象可以独立确定它是否等于另一个对象.
Set
s essentially use ===
for comparison of values stored in the set. If you want to solve this yourself you will have to resort to looping or creating your own set data structure. When you make your solution you can make it dependent on an equals
method so that each object can independently determine if it is equal to another.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set