0

(This is a follow up question from Empty set in javascript)

I have a set in javascript defined like this:

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

I initialize one set like this:

myset = createSetFromList(["apple", "orange", "banana"]);

Now I would like to see the properties of that set. I try with this:

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

I now try to see the defined properties:

showProperties(myset);

The only output I get, no matter how I initialize the array, is:

undefined belongs

What is going on? How can I walk the set elements?

Community
  • 1
  • 1
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • I tried your example, and it works for me. It prints everything as belongs. UPDATE: tried on Chrome's console. – jeffreyveon Nov 02 '11 at 07:02
  • 1
    Must have something to do with `$.log`? Tried it here, works like expected. Try it out using `console.log` ... – KooiInc Nov 02 '11 at 07:06
  • I have also tried with console.log, same result. I am using Chrome too, and the javascript is being served from couchdb. – blueFast Nov 02 '11 at 07:12
  • Works for me to. I tried this [fiddle](http://jsfiddle.net/karalamalar/ra7mk/) on IE7, IE8, Chrome, Firefox, Safari and Opera and there is no problem. – Emre Erkan Nov 02 '11 at 07:13
  • It works in all browsers I try it in here: http://jsfiddle.net/jfriend00/UuWn8/. There must be something wrong in the code that you aren't showing us. – jfriend00 Nov 02 '11 at 07:22
  • JavaScript doesn't have sets. `{}` creates an *object*. – ThiefMaster Nov 02 '11 at 07:32
  • Correct, javascript does not have a set type. That is why I *simulate* it with objects. – blueFast Nov 02 '11 at 07:43

1 Answers1

-1

Your code seems correct. It may be that you're trying this in a browser that doesn't support hasOwnProperty for JSObject. Instead, you could try this, which should be more cross-browser friendly:

function showProperties(v) {
    for (x in v) {
        if (v[x] != undefined) {
            $.log(x + " belongs");
        } else {
            $.log(x + " does not belong");
        }
    } 
}

The only other comment I'd make is that the 'if else' check in the 'for x in v' is unnecessary because the for loop will only return expand properties of the Object that you added.

pra9ma
  • 388
  • 1
  • 4
  • I have used your version, same result: "undefined belongs". I am using Chome 15.0.874.106 on Ubuntu. Server is couchdb 1.1.0. I agree with your if/else comment, but since I get strange behaviour, I must do strange things :) – blueFast Nov 02 '11 at 07:19
  • Very strange. if (v[x] != undefined), I'm not sure how it could possibly print "undefined belongs" since it was just tested to confirm it wasn't undefined. You could try modifying the if as follows: if (v[x] && v[x] != "undefined") – pra9ma Nov 02 '11 at 07:34
  • Does this work on another browser? Also, I wonder if you have installed any extension or script which might be interfering.. Try running it in incognito mode as well... – jeffreyveon Nov 02 '11 at 17:40