0

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 working too

if(items.hasOwnProperty(val)){}

Please help

Nick
  • 1,799
  • 3
  • 23
  • 32
  • There are some _relevant_ answers here http://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-which-method-is-better, as well as here http://stackoverflow.com/questions/7636789/in-javascript-is-there-an-easier-way-to-check-if-a-property-of-a-property-exists – Michael Jasper Oct 04 '11 at 05:14

1 Answers1

0

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);
});
Headshota
  • 21,021
  • 11
  • 61
  • 82