0

I have a object

var foo = { jon : {age: 'old',   feeling: 'sad' },
            jack: {age: 'young', feeling: 'happy'}
          };

The members of foo are created dynamically; as is the object bar.

var bar = { name: 'jon' };

I would like to to see if jon is a member of foo. I must be doing something terribly wrong.

function isMember(bar) {
    for(prop in foo) {
        if (foo[prop] === bar.name){
            return true;
            break;
        }
    }
    return false;
};

always returns true!!

JJRutter
  • 85
  • 3
  • 14
  • The JS you posted is messy and not really valid JS. Are you sure this is exactly what your function looks like? –  Mar 20 '12 at 00:50
  • There are _so many_ syntax errors. Who knows what it will `return`. – calebds Mar 20 '12 at 00:52
  • @paislee there is one syntax error, and one other thing that might or might not be a problem. How is this *so many*? – Yoshi Mar 20 '12 at 00:54
  • The author has edited the error with the JSON assignment at the beginning, but still hasn't corrected the function declaration (see the errant equal sign?), yet. The code as demonstrated (even now) won't run at all, let alone always return true. –  Mar 20 '12 at 00:57
  • David, thats why I'm looking for help... – JJRutter Mar 20 '12 at 01:01
  • Whoops, I missed that '=' in my isMember function... – JJRutter Mar 20 '12 at 01:03
  • @Yoshi: There was a missing comma in `foo` property list, undefined `jon`, unexpected `=` in the function dec, and unreachable code. I stopped there.. I'll give you some hyperbole in my comment though ;) – calebds Mar 20 '12 at 01:06

3 Answers3

4

Here's a working version of the above:

foo = { jon : {age: 'old',   feeling: 'sad' },
        jack: {age: 'young', feeling: 'happy'}
       };
bar = { name: 'jon' };

function isMember(bar) {
    for(prop in foo) {
        console.log([prop, foo[prop], bar.name, ]);
        if (prop === bar.name){
            return true;
        }
    }
    return false;
 };


console.log(['ismember', isMember(bar)]);

Live example:

Try changing bar.name to e.g. jon1 to see how it behaves. Take a look at your console window to see what it outputs, that will help you understanding what it does exactly and where you went wrong.

Hope this helps.

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
1

It's so simple it's hardly worth using a function:

console.log(bar.name in foo);

Live demo

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
  • Or for that matter, `foo[bar.name] !== undefined` – Weston C Mar 20 '12 at 01:10
  • `foo[bar.name] !== undefined` is popular but unfortunately not thoroughly reliable @Weston. However `typeof foo[] == "undefined"` is. It's well discussed in [this topic][http://stackoverflow.com/questions/776950/javascript-undefined-undefined] – Beetroot-Beetroot Mar 20 '12 at 01:27
  • 1
    My way of thinking is a little more like along [the discussion here](http://stackoverflow.com/questions/8783510/javascript-how-dangerous-is-it-really-to-assume-undefined-is-not-overwritten), but if somebody else wants to use `typeof` just in case, more power to 'em. Or the method in your answer, for that matter -- more concise and avoids any potential `undefined` shenanigans. – Weston C Mar 20 '12 at 01:45
  • Ooh interesting. From that I learn that "The ES5 standard dictates that `undefined` is now readonly", which (if correct) makes your test more reliable than I thought - just older browsers to worry about. Thanks for that one @Weston. – Beetroot-Beetroot Mar 20 '12 at 01:57
0

Once the syntax is cleared up, I think you're looking for the built-in hasOwnProperty method for objects.

var foo = {
    "jon": {
        "age": "old",
        "feeling": "sad"
    },
    "jack": {
        "age": "young",
        "feeling": "happy"
    }
};
var bar = {
    "name": "jon"
};
function isMember(prop) {
    return foo.hasOwnProperty(prop);
}
console.log(isMember(bar.name));

Working fiddle at: http://jsfiddle.net/Dwr2M/

pete
  • 24,141
  • 4
  • 37
  • 51