最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Empty set in javascript - Stack Overflow

programmeradmin2浏览0评论

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

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
Share Improve this question edited Nov 1, 2011 at 9:38 blueFast asked Nov 1, 2011 at 8:09 blueFastblueFast 44.7k66 gold badges214 silver badges374 bronze badges 4
  • possible duplicate of How do I test for an empty Javascript object from JSON? – Lightness Races in Orbit Commented Nov 1, 2011 at 8:24
  • Note that object property names must be strings or numbers, so you must overload toString to return a unique string for any object that may be added to a set. – outis Commented Nov 1, 2011 at 8:35
  • Numbers are actually toString'd also, ie when used in arrays – meandmycode Commented Nov 1, 2011 at 10:57
  • The behavior of showProperties you ask about in the edit is a different issue, and thus deserves a different question. It may be related to the issue I mention in my first ment, namely that generic objects aren't suitable for property names, since names must be strings. – outis Commented Nov 3, 2011 at 2:33
Add a ment  | 

2 Answers 2

Reset to default 2

Best I have is

var isEmptyObject = function(v) { 
   for (x in v) {
     if (v.hasOwnProperty(x)) {
          return false;
     }
   } 
   return 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;
发布评论

评论列表(0)

  1. 暂无评论