开发者

Javascript : How to check if a key exists and return the value of the key

I want to check if a key exist in an object and if exist return the value of the key.

var user_right=user_rights.split(',');
var tbar=new Array();
Ext.each(user_right,function(val,index){
    if(items.hasOwnProperty(val))
    -->tbar.push(items.val)
});
console.log(tbar);

But 'tbar.push(items.val)' is not working I'm sure that this is not the right method. How can retrieve value. Update : Unfortunately this is below code is not wo开发者_如何学编程rking too

if(items.hasOwnProperty(val)){}

Please help


I assume that you are checking values of the object with hasOwnProperty, which checks the keys not values.

you can use ordinary for loop:

for(var i=0; i<user_right.length; i++){
   tbar[i] = user_right[i];
}

you'd better use Ext.iterate for non array objects:

Ext.iterate(user_right, function(key, value) {
  if(items.hasOwnProperty(key))
    tbar.push(value);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜