简体   繁体   English

如何检查object中的object是否存在

[英]How to check if object within object exists

It seems that the following technique for checking the existence of an object member produces an error because the 'bar' parent object hasn't been declared before the check, which means I either have to declare it before the check or use two 'typeof' expressions, either of which would be excess code:似乎以下用于检查 object 成员是否存在的技术会产生错误,因为在检查之前尚未声明“bar”父 object,这意味着我必须在检查之前声明它或使用两个“typeof”表达式,其中任何一个都是多余的代码:

var foo = {},
    newVal = (typeof foo.bar.myVal !== 'undefined' ? foo.bar.myVal : null );

Error: foo.bar is undefined

So, how do you check if a member within an undeclared object exists without producing an error?那么,如何检查未声明的 object 中的成员是否存在而不产生错误?

I love javascript, but sometimes...我喜欢 javascript,但有时......

It can be done simply using the code below:只需使用以下代码即可完成:

var newVal = (foo && foo.bar && typeof foo.bar.myVal !== 'undefined') ? foo.bar.myVal : foo.bar.myVal

A property is null or undefined, it will be evaluated as false so the above code will only process up to the first 'false' statement.一个属性是 null 或未定义,它将被评估为 false,因此上述代码将只处理第一个“false”语句。

var newVal = ('foo' in window && // could be typeof foo !== 'undefined' if you want all scopes
             'bar' in foo &&
             'myVal' in foo.bar) ? foo.bar.myVal : null;

To be fair to javascript, that reads almost like natural language.公平地说,javascript 读起来几乎像自然语言。

The simplest test is:最简单的测试是:

if (foo && foo.bar) {
  // play with foo.bar.myVal  ... 
}

As Matthew Abbott gave in his response, you can write a method for it to use it later if you do a lot of diving into child properties where the whole object or somewhere along the chain can be null.正如Matthew Abbott在他的回复中给出的那样,如果您对子属性进行了大量深入研究,其中整个 object 或沿链的某个位置可以是 null,您可以编写一个方法供以后使用。 Below is my implementation using lodash .下面是我使用lodash的实现。

checkGraph(obj, graphPath) {
    if (obj) {
      let root = obj;
      _.each(graphPath.split('.'), part => {
        if (root[part]) {
          root = root[part];
        } else {
          return false; // bad property
        }
      });
      return true;
    }
    return false; // bad object
  }

You can call the method like this:您可以像这样调用该方法:

if (this.checkGraph(object, 'property.subproperty') {
    // do thing
}

You could wrap it up into a method:你可以把它包装成一个方法:

function checkGraph(obj, graphPath)
{
  var parts = graphPath.split(".");
  var root = obj;

  for (var i = 0; i < parts.length; i++)
  {
    var part = parts[i];
    if (root[part] && root.hasOwnProperty(part))
      root = root[part];
    else
      return false;
  }

  return true;
}

Where you could call it as:您可以将其称为:

var foo = {},
    newVal = checkGraph(foo, "bar.myVal") ? foo.bar.myVal : null;

There is another way is you use a JS transpiler like Babel :还有另一种方法是使用像Babel这样的JS 转译器

if (foo?.bar) {
  // play with foo.bar.myVal  ... 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM