如何在Javascript中检查集合是否包含数组? [英] How to check if a set contains an array in Javascript?

查看:64
本文介绍了如何在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 方法,以便每个对象可以独立确定它是否等于另一个对象.

Sets 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

这篇关于如何在Javascript中检查集合是否包含数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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