-1

以下のようなオブジェクトがあります。

var _obj = { 
    'name1@gmail.com' : { 
        'bob@gmail.com' : {
            channel: 'V2231212', 
            from: 'bob'
        },
        'judy@gmail.com' : {
            channel: 'V223231212', 
            from: 'judy'
        }
    },
    'name2@gmail.com' : { 
        'bill@gmail.com' : {
            channel: 'V123123',
            from: 'bill'
        }
    }
};

channelオブジェクト内のどこかに " "と等しい" " が存在するかどうかを確認するにはどうすればよいV123123ですか?

上記の場合、bill@gmail は V123123 に等しく、true を返す必要があります。

何か案は?

4

3 に答える 3

0

さらに別の解決策

Object.keys(_obj).forEach(function(v, i){
  Object.keys(_obj[v]).forEach(function(w, k){
    if(_obj[v][w].hasOwnProperty('channel')&&_obj[v][w]['channel']=='V123123')
      return true;
  })
})
于 2013-03-21T09:34:40.803 に答える
0

それを試してみてください:

console.log(containsPropertyValue(_obj, 'channel', 'V123123'));

function containsPropertyValue(obj, propertyName, propertyValue){
    var contains = false;
    function a(o){
        for(var prop in o)
            if(o.hasOwnProperty(prop)){
                if(prop === propertyName && o[prop] === propertyValue){
                    contains = true;
                    return;
                }
                if(typeof o[prop] === 'object')
                    a(o[prop]);
            }
    }
    a(_obj);
    return contains;
}

JSFiddle

于 2013-03-21T09:10:19.190 に答える
0

Another alternative using getOwnPropertyNames

function hasPropertyWithValue(obj, propertyName, propertyValue) {
    var matchFound = false;
    var _prop;
    var _properties = Object.getOwnPropertyNames(obj);

    for(var i = 0; i < _properties.length; i++){
        _prop = _properties[i];

        if(_prop === propertyName && obj[_prop] === propertyValue){
            return true;
        }

        if(typeof obj[_prop] === 'object'){
            matchFound = hasPropertyWithValue(obj[_prop],  propertyName, propertyValue);
            if (matchFound){
               return true;
            }
        }
    }

    return matchFound;
}

Used like this:

var matched = hasPropertyWithValue(_obj, 'channel', 'V123123');

DEMO - Using getOwnPropertyNames()



getOwnPropertyNames() Browser Support


  • Firefox (Gecko) : 4 (2.0)
  • Chrome : 5
  • Internet Explorer : 9
  • Opera : 12
  • Safari : 5
于 2013-03-21T10:16:24.183 に答える