在javascript中,测试属性是否深深嵌套在对象图中?(In javascript, test for property deeply nested in object graph?)
我从CouchDB数据库中获得了一组完全不同的复杂JSON对象。 每个包含很多级别的嵌套属性 - 例如,
tps_report.personnel_info.productivity.units_sold = 8
我想通过这些对象进行迭代并对它们进行处理:例如,
// writes units sold from each TPS report: for (i in tpsReports) { if (tpsReports[i].personnel_info.productivity.units_sold < 10) { fireEmployee(); } }
问题是许多TPS报告没有设置所有这些属性。 因此,如果我尝试这样做,那么在第一次循环获取到没有“personnel_info”属性的报告时,会出现错误,从而尝试查找“undefined”的“productivity”属性。 我宁愿发生的是条件只是跳过它并继续。
我看到了两种方式,这两种方式对我来说都显得很丑陋
- 分别使用嵌套条件测试每个属性
- 将行放在try / catch块中以捕获错误并忽略它
我更喜欢的是PHP的isset()函数,它不会抛出错误,无论提供什么内容 - 它只会告诉你你正在寻找的特定变量是否存在。 所以,就像
// writes units sold from each TPS report: for (i in tpsReports) { if (isset(tpsReports[i].personnel_info.productivity.units_sold)){ if (tpsReports[i].personnel_info.productivity.units_sold < 10) { fireEmployee(); } } }
有什么想法吗?
I've got a collection of disparate, complex JSON objects from a CouchDB database. Each contains many levels of nested properties--for example,
tps_report.personnel_info.productivity.units_sold = 8
I want to iterate through these objects and do stuff with them: for instance,
// writes units sold from each TPS report: for (i in tpsReports) { if (tpsReports[i].personnel_info.productivity.units_sold < 10) { fireEmployee(); } }
The problem is that many TPS reports don't have all these properties set. So if I try this, I'll get an error the first time the loop gets to a report without the "personnel_info" property and thus tries to find the "productivity" property of "undefined." What I'd rather happen is that the conditional just skips it and continues.
I see two ways around this, both of which seem ugly to me
- test for each property separately with nested conditionals
- enclose the line in a try/catch block to catch the error and ignore it
What I'd prefer would be something like PHP's isset() function, which won't throw an error regardless of what you feed it--it'll just tell you whether the particular variable you're looking for exists or not. So, like
// writes units sold from each TPS report: for (i in tpsReports) { if (isset(tpsReports[i].personnel_info.productivity.units_sold)){ if (tpsReports[i].personnel_info.productivity.units_sold < 10) { fireEmployee(); } } }
Any thoughts?
原文:https://stackoverflow.com/questions/4343028
满意答案
尝试使用当前年份的
'c'
,如:$('#dobDate').datepicker({yearRange : '1920:c',changeYear: true});
也使用
changeYear: true
工作演示Fiddle.net
Try using
'c'
for the current year like:$('#dobDate').datepicker({yearRange : '1920:c',changeYear: true});
use also the
changeYear: true
working demo Fiddle.net
相关问答
更多jQuery datepicker年显示(jQuery datepicker years shown)
jQuery UI:Datepicker设置年份范围下拉到100年(jQuery UI: Datepicker set year range dropdown to 100 years)
如何将Bootstrap Datepicker日期视图限制为Year(How to limit Bootstrap Datepicker date view to just Year)
Datepicker - 仅限年份?(Datepicker - year only?)
展示未来的一年(Display year in the future)
如何在datepicker下拉列表中显示从1920年到当年的正确年份(How to display correct years from 1920 to current year in datepicker drop down for year)
jQuery DatePicker:如何将year数组传递给yearRange下拉列表(jQuery DatePicker: How to pass array of years to the yearRange drop down)
jQueryUI DatePicker自定义年份(jQueryUI DatePicker Customizing the Year)
JQuery Datepicker - 添加年份(JQuery Datepicker - Add Years)
jQuery Datepicker将默认日期设置为“当前月份,当前日期,(当前年份-18)”(jQuery Datepicker Set Default Date to “current month, current day, (current year-18)”)
相关文章
更多javascript 问题
Object Oriented Programming
请教javascript问题
could not find system property or JNDI
Guava学习笔记:复写的Object常用方法
javascript快速入门
Cannot expose request attribute 'website' because of an existing model object of the same name 的解决方案
Hadoop 0.21如何运行单个test case
从 prototype.js 深入学习 javascript 的面向对象特性
JavaScript实现长按功能问题
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!