JavaScript 中的空集

发布于 2024-12-13 13:09:25 字数 1159 浏览 7 评论 0原文

我已经在基于通用对象类型的 javascript 中实现了一个集合数据类型,如下所示:

function createSetFromList(list) {
    var set = { };
    for (var i = 0; i < list.length; i++)
        set[list[i]] = true;
    return set;
}

现在我可以轻松检查给定值是否属于该集合:

var users = createSetFromList(my_users);
if (user in users) allow_operation = true;

我遇到的问题是我想检查我的集合是否为空,像这样:

if ("users is empty" or user in users) allow_operation = true;

但我不知道如何检查集合是否为空。我尝试过:

if (users == { } || user in users) allow_operation = true;

但显然逻辑表达式的第一部分从来​​都不是真的。

我想这与以下事实有关:当 users 为空时,它仍然被初始化为一个对象,没有任何集合元素,并且一个对象永远不等于另一个对象?

是否有任何解决方法可以检查我的设置实现是否为空?

编辑:我已经尝试过马伏里奥的建议,但发生了一些奇怪的事情。我对它进行了一些修改,看看发生了什么:

function showProperties(v) {
    for (x in v) {
        if (v.hasOwnProperty(x)) {
            $.log(x + " belongs");
        } else {
            $.log(x + " does not belong");
        }
    } 
}

运行此命令时:

showProperties(myset);

我总是只得到一行,无论我的集使用哪些数据进行了初始化:

undefined belongs

I have implemented a set datatype in javascript based in a generic object type, like this:

function createSetFromList(list) {
    var set = { };
    for (var i = 0; i < list.length; i++)
        set[list[i]] = true;
    return set;
}

Now I can easily check whether a given value belongs to the set:

var users = createSetFromList(my_users);
if (user in users) allow_operation = true;

The problem that I have is that I would like to check if my set is empty, like this:

if ("users is empty" or user in users) allow_operation = true;

But I have no idea how to check if the set is empty. I have tried with:

if (users == { } || user in users) allow_operation = true;

But apparently the first part of the logical expression is never true.

I guess it has to do with the fact that when users is empty, it is still initialized as an object, without any set elements, and an object is never equal to another object?

Is there any workaround to check for emptiness for my set implementation?

EDIT: I have tried out Malvolio's suggestion, and something strange is going on. I have modified it a bit to see what is happening:

function showProperties(v) {
    for (x in v) {
        if (v.hasOwnProperty(x)) {
            $.log(x + " belongs");
        } else {
            $.log(x + " does not belong");
        }
    } 
}

When running this:

showProperties(myset);

I always get only one line, regardless with which data my set has been initialized:

undefined belongs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-12-20 13:09:25

我最好的是

var isEmptyObject = function(v) { 
   for (x in v) {
     if (v.hasOwnProperty(x)) {
          return false;
     }
   } 
   return true;
};

Best I have is

var isEmptyObject = function(v) { 
   for (x in v) {
     if (v.hasOwnProperty(x)) {
          return false;
     }
   } 
   return true;
};
夜未央樱花落 2024-12-20 13:09:25

您可以使用数组来完成您的任务。

或者,如果您需要对 set 使用特殊类型的操作,您可以将 set 定义为:

function SetOfValues(list) {
    if(!(this instanceof SetOfValues))
       return arguments.length===1?new SetOfValues(list):new SetOfValues;
    if(arguments.length===1)for(var i=0;i<list.length;i++)this[list[i]]=true;
}
SetOfValues.prototype.in=function(item) {
  return this.hasOwnProperty(item);
}
SetOfValues.prototype.empty=function() {
  for(var p in this)if(this.hasOwnProperty(p))return false;
  return true;
}

,然后创建并使用 set 作为:

var users = new SetOfValues(my_users);
// or with help of SetOfValues definition simply:
// var users = SetOfValues(my_users);
if(users.in(user)) allow_operation = true;
if(users.empty()||users.in(user)) allow_operation = true;

You can use Array for your task.

Or if you need to use special kind of operations on set you can define set as:

function SetOfValues(list) {
    if(!(this instanceof SetOfValues))
       return arguments.length===1?new SetOfValues(list):new SetOfValues;
    if(arguments.length===1)for(var i=0;i<list.length;i++)this[list[i]]=true;
}
SetOfValues.prototype.in=function(item) {
  return this.hasOwnProperty(item);
}
SetOfValues.prototype.empty=function() {
  for(var p in this)if(this.hasOwnProperty(p))return false;
  return true;
}

and then create and use set as:

var users = new SetOfValues(my_users);
// or with help of SetOfValues definition simply:
// var users = SetOfValues(my_users);
if(users.in(user)) allow_operation = true;
if(users.empty()||users.in(user)) allow_operation = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文