在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
相关问答
更多-
如果您往下看演示页面,您将看到一个“限制日期戳”部分。 使用下拉列表来指定“ Year dropdown shows last 20 years ”演示,并点击查看源: $("#restricting").datepicker({ yearRange: "-20:+0", // this is the option you're looking for showOn: "both", buttonImage: "templates/images/calendar.gif", ...
-
jQuery UI:Datepicker设置年份范围下拉到100年(jQuery UI: Datepicker set year range dropdown to 100 years)[2022-08-19]
您可以使用此选项在每个文档中设置年份范围http://api.jqueryui.com/datepicker/#option-yearRange yearRange: '1950:2013', // specifying a hard coded year range 或这样 yearRange: "-100:+0", // last hundred years 从文档 默认值:“c-10:c + 10” 相对于当前选定年份(“c-nn:c + nn”),绝对(“nnnn: nnnn“)或这些格式的组合 ... -
如何将Bootstrap Datepicker日期视图限制为Year(How to limit Bootstrap Datepicker date view to just Year)[2021-11-30]
试试这个解决方案 $('.datepicker').datepicker({ autoclose: true, format: " yyyy", viewMode: "years", minViewMode: "years", startDate: '2014', endDate: new Date(), }); 见JsFiddle 。 我希望它适合你,谢谢。 Try this solution : $('.datepicker').datepicker( ... -
Datepicker - 仅限年份?(Datepicker - year only?)[2023-07-12]
Silverlight for Windows Phone Toolkit中的DatePicker控件没有这种直接自定义! 您必须更改选择器控件及其使用的完整页面; 你可以在这里查看有关DatePicker定制的一些信息,虽然它很稀疏...... The DatePicker control in the Silverlight for Windows Phone Toolkit doesn't have that kind of direct customization! You'll have to c ... -
展示未来的一年(Display year in the future)[2022-06-22]
试试这个,这会产生一个从2016年到今年的年份列表 WITH CTE_TEST AS( SELECT 2016 AS NYEAR UNION ALL SELECT NYEAR+1 FROM CTE_TEST WHERE NYEAR+1 <= YEAR(GETDATE()) ) SELECT * FROM CTE_TEST Try this, this will generate a list of Year from 2016 to current year ... -
尝试使用当前年份的'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: tr ...
-
根据https://api.jqueryui.com/datepicker/#option-yearRange - 根据文档,这是不可能的。 您只能设置一个连续的范围。 但是,这个问题真的让我们直接回到了前面问题中讨论的XY问题 - 基本上,如果您的结束日期为空而不是使用假装日期,那么您首先不会遇到这个问题。 而且你的应用程序的数据质量会更好。 我已经知道你不想听到这些,但这是问题的根源。 如果您无法更改数据库中的内容,那么至少在UI层中删除这些日期 As per https://api.jqueryui. ...
-
点击这里观看演示。 当您发现自己超出范围时,jQuery UI源很容易修改。 您只需要更改_generateMonthYearHeader的函数定义: // StackOverflow question: Add option for N/A html += ''; 并将此更改改为_selectDay以省略选择N / A的年份。 // StackOverflow question: format the date if (inst.curr ...
-
尝试使用Date的setFullYear和getFullYear : $("#equipment_purchaseDate").datepicker({ dateFormat: 'dd/mm/yy', onSelect: function(dateStr) { var d = $.datepicker.parseDate('dd/mm/yy', dateStr); var years = parseInt($("#equipment_warrantyLengt ...
-
尝试 var d = new Date(); var year = d.getFullYear() - 18; d.setFullYear(year); $('#BirthDate').datepicker({ changeYear: true, changeMonth: true, yearRange: '1920:' + year + '', defaultDate: d}); 演示: 小提琴 Try var d = new Date(); var year = d.getFullYear() - 1 ...