[英]Check if object member exists in nested object
Is there a simpler way than using ___ in object
to check the existence of each level of an object to check the existence of a single member? 有没有比
___ in object
使用___ in object
更简单的方法来检查___ in object
的每个级别是否存在以检查单个成员的存在?
More concisely: How can I check if someObject.member.member.member.value exists? 更简洁:我如何检查someObject.member.member.member.value是否存在?
In general, you can use if(property in object)
, but this would be still cumbersome for nested members. 通常,您可以使用
if(property in object)
,但这对于嵌套成员来说仍然很麻烦。
You could write a function: 你可以写一个函数:
function test(obj, prop) {
var parts = prop.split('.');
for(var i = 0, l = parts.length; i < l; i++) {
var part = parts[i];
if(obj !== null && typeof obj === "object" && part in obj) {
obj = obj[part];
}
else {
return false;
}
}
return true;
}
test(someObject, 'member.member.member.value');
You could also try/catch TypeError? 你还可以尝试/捕获TypeError?
try {
console.log(someObject.member.member.member.value);
} catch(e) {
if (e instanceof TypeError) {
console.log("Couldn't access someObject.member.member.member.value");
console.log(someObject);
}
}
Here's one way: http://jsfiddle.net/9McHq/ 这是一种方式: http : //jsfiddle.net/9McHq/
var result = ((((someObject || {}).member || {}).member || {}).member || {}).value;
alert( result );
Check out lodash-deep's deepHas
https://github.com/marklagendijk/lodash-deep#_deephascollection-propertypath 查看lodash-deep的
deepHas
https://github.com/marklagendijk/lodash-deep#_deephascollection-propertypath
And this too https://github.com/danielstjules/hoops 这也是https://github.com/danielstjules/hoops
Theres a safeRead function defined here on thecodeabode which allows a safeRead of nested properties ie 这是一个在codeabode上定义的safeRead函数,它允许一个safeRead的嵌套属性,即
safeRead(foo, 'bar', 'jim', 'jam');
if any of the properties are null or undefined a blank string is returned - useful for formatting / string interpolation 如果任何属性为null或未定义,则返回空字符串 - 对格式化/字符串插值很有用
Something like (warning: untested code ahead) 像(警告:未经测试的代码)
var testProperty = function(obj, proplist) {
for (var i=0; i < proplist.length; i++) {
if (obj.hasOwnProperty(proplist[i])) {
obj = obj[proplist[i]];
} else {
return false;
}
}
return true;
}
if (someObject.member && someObject.member.member &&
someObject.member.member.member && someObject.member.member.member.value) ...
or similarly: 或类似的:
var val = foo.bar && foo.bar.jim && foo.bar.jim.jam && foo.bar.jim.jam.value;
This will not 'work' if any particular value happens to be null
, false
, 0
, or ""
(an empty string), but with the possible exception of the final value, this seems unlikely to be the case. 如果任何特定值恰好是
null
, false
, 0
或""
(空字符串),这将不会“起作用”,但是除了最终值之外,这似乎不太可能。
Also, note that typeof ____ !== "undefined"
is not the correct test to see if an object has a property. 另请注意,
typeof ____ !== "undefined"
不是正确的测试,以查看对象是否具有属性。 Instead you should use ___ in object
, eg if ("member" in someObject)
. 相反,你应该
___ in object
使用___ in object
,例如if ("member" in someObject)
。 This is because you can set a property to an explicit value of undefined
, which is different from removing it with delete someObject.member
. 这是因为您可以将属性设置为
undefined
的显式值,这与使用delete someObject.member
删除它不同。
If you can use lodash
library, it has a very elegant solution, hasIn . 如果你可以使用
lodash
库,它有一个非常优雅的解决方案, hasIn 。
_.hasIn(someObject, 'member.level1.level2.level3');
for example, 例如,
var obj = {'x': 999, 'a': {'b': {'c': 123, 'd': 234}}}
// => undefined
_.hasIn(obj, 'x')
// => true
_.hasIn(obj, 'a.b.d')
// => true
_.hasIn(obj, 'a.b.e')
// => false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.