javascript 检查值是否与对象匹配

发布于 2024-10-08 12:14:44 字数 206 浏览 1 评论 0原文

我有一个 javascript 对象

var obj = {
    "0" : "apple",
    "1" : "pear",
    "2" : "orange"
}

,我想检查 obj 中是否有“orange”。

是否有内置函数可以执行此操作?或者我应该迭代 obj 的每个值?

谢谢。

I have a javascript object

var obj = {
    "0" : "apple",
    "1" : "pear",
    "2" : "orange"
}

I want to check if 'orange' is in obj.

Is there a built in function that does this? Or should I iterate over each value of obj?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

浊酒尽余欢 2024-10-15 12:14:44

您必须迭代:

for (var k in obj) {
  if (!obj.hasOwnProperty(k)) continue;
  if (obj[k] === "orange") {
    /* yaay! an orange! */
  }
}

现在“hasOwnProperty”测试是为了确保您不会绊倒从原型继承的属性。在某些情况下,这可能是不可取的,而且实际上这是您需要了解的事情之一,以便确定您是否愿意进行该测试。您从对象原型中获取的属性有时可能是各种库放置在那里的东西。 (我认为 ES5 标准提供了控制此类属性是否“可迭代”的方法,但在现实世界中仍然存在 IE7。)

You'll have to iterate:

for (var k in obj) {
  if (!obj.hasOwnProperty(k)) continue;
  if (obj[k] === "orange") {
    /* yaay! an orange! */
  }
}

Now that "hasOwnProperty" test in there is to make sure you don't stumble over properties inherited from the prototype. That might not be desirable in some cases, and really it's one of the things you kind-of need to understand in order to know for sure whether you do or don't want to make that test. Properties that you pick up from an object's prototype can sometimes be things dropped there by various libraries. (I think the ES5 standard provides for ways to control whether such properties are "iterable", but in the real world there's still IE7.)

新人笑 2024-10-15 12:14:44

没有内置函数可以执行此操作,您必须检查每个属性。

另外,从对象的外观来看,它应该是一个数组而不是一个对象。如果是这种情况,迭代这些值会稍微容易一些,并且效率会稍微更高。

var arr = ['apple','pear','orange'];
function inArray(inVal){
    for( var i=0, len=arr.length; i < len; i++){
        if (arr[i] == inVal) return true;
    }
    return false;
}

There is no built in function to do this, you'd have to check each property.

Also by the looks of your object, it should be an array instead of an object. If that was the case it would be slightly easier to iterate through the values and be marginally more efficient.

var arr = ['apple','pear','orange'];
function inArray(inVal){
    for( var i=0, len=arr.length; i < len; i++){
        if (arr[i] == inVal) return true;
    }
    return false;
}
阳光的暖冬 2024-10-15 12:14:44
function has(obj, value) {
  for(var id in obj) {
    if(obj[id] == value) {
      return true;
    }
  }
  return false;
}

if(has(obj, "orange")) { /* ... */ }
function has(obj, value) {
  for(var id in obj) {
    if(obj[id] == value) {
      return true;
    }
  }
  return false;
}

if(has(obj, "orange")) { /* ... */ }
空袭的梦i 2024-10-15 12:14:44

您可能想查看 jQuery.inArray。认为它也适用于物体。

You might wanna look at jQuery.inArray. Think it works for objects aswell.

红焚 2024-10-15 12:14:44
const TESTobj = ( Obj, Value ) => {
    let IsBool = 0;
    Object.values(Obj).forEach((val) => { if(val===Value){ IsBool++; }; });
    if( IsBool ){ return true; } else{ return false; };
};

var obj = { "0": "apple", "1": "pear", "2": "orange" };

console.log( TESTobj( obj, `orange` ) );
// return true;

console.log( TESTobj( obj, `peach` ) );
// return false;
const TESTobj = ( Obj, Value ) => {
    let IsBool = 0;
    Object.values(Obj).forEach((val) => { if(val===Value){ IsBool++; }; });
    if( IsBool ){ return true; } else{ return false; };
};

var obj = { "0": "apple", "1": "pear", "2": "orange" };

console.log( TESTobj( obj, `orange` ) );
// return true;

console.log( TESTobj( obj, `peach` ) );
// return false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文