Javascript:如何检查键是否存在并返回键的值

发布于 2024-12-07 18:57:47 字数 393 浏览 1 评论 0原文

我想检查对象中是否存在某个键,如果存在则返回该键的值。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2024-12-14 18:57:47

我假设您正在使用 hasOwnProperty 检查对象的值,它检查键而不是值。

您可以使用普通的 for 循环:

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

对于非数组对象,您最好使用 Ext.iterate :

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

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);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文