Javascript:如何检查键是否存在并返回键的值
我想检查对象中是否存在某个键,如果存在则返回该键的值。
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);
但是 'tbar.push(items.val)' 不起作用我确信这不是正确的方法。 如何找回价值。 更新:不幸的是,下面的代码也不起作用
if(items.hasOwnProperty(val)){}
请帮忙
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在使用 hasOwnProperty 检查对象的值,它检查键而不是值。
您可以使用普通的 for 循环:
对于非数组对象,您最好使用 Ext.iterate :
I assume that you are checking values of the object with hasOwnProperty, which checks the keys not values.
you can use ordinary for loop:
you'd better use Ext.iterate for non array objects: