Check if JavaScript object contains X?

javascript

Solution

You have to loop through the object to test it:

var chk = false;
for(var key in map){
    if(map[key] == "here"){
        chk = true;
        break;
    }
}
alert(chk?"Yup":"Nah");

You can also put this in `Object` prototype:

Object.prototype.ifExist = function(txt){
    var chk = false;
    for(var key in this){
        if(this[key] == txt){
            chk = true;
            break;
        }
    }
    return chk;
}

//map.ifExist("here");
//return true

Demo: http://jsfiddle.net/DerekL/yWnYy/

Problem

I need to know if a JavaScript object contains a certain variable. EG: Check if 'map' contains 'here' ``` var map = { '10': '0', '20': '0', '30': 'here', }, ```

Original source