javascript 如何检查对象中的对象是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6548256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if object within object exists
提问by Steve
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:
似乎以下用于检查对象成员是否存在的技术会产生错误,因为在检查之前尚未声明“bar”父对象,这意味着我必须在检查之前声明它或使用两个“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?
那么,如何在不产生错误的情况下检查未声明对象中的成员是否存在?
I love javascript, but sometimes...
我喜欢 javascript,但有时...
回答by Dino Gambone
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' 语句。
回答by davin
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 读起来几乎像自然语言。
回答by RobG
The simplest test is:
最简单的测试是:
if (foo && foo.bar) {
// play with foo.bar.myVal ...
}
回答by Matthew Abbott
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;
回答by Elijah Fry
As Matthew Abbottgave 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. Below is my implementation using lodash.
正如Matthew Abbott在他的回应中给出的那样,如果您深入研究整个对象或链中某处可以为 null 的子属性,您可以编写一个方法供以后使用。下面是我使用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
}
回答by Gabrielizalo
There is another way is you use a JS transpilerlike Babel:
还有另一种方法是你使用像Babel这样的JS 转译器:
if (foo?.bar) {
// play with foo.bar.myVal ...
}