在javascript中,测试属性深深地嵌套在对象图中? [英] In javascript, test for property deeply nested in object graph?

查看:83
本文介绍了在javascript中,测试属性深深地嵌套在对象图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从CouchDB数据库收集了一些完全不同的复杂JSON对象。每一个都包含很多级别的嵌套属性 - 例如,

  tps_report.personnel_info.productivity.units_sold = 8 

我想遍历这些对象并对它们进行操作:例如,

  //写出从每个TPS报告中销售的单位:
for(i in tpsReports){
if(tpsReports [i] .personnel_info.productivity。 units_sold< 10){
fireEmployee();




$ b $ p
$ b

问题在于许多TPS报告没有所有这些属性设置。因此,如果我尝试这样做,那么在第一次循环获取到没有personnel_info属性的报告时,会出现错误,从而尝试查找undefined的productivity属性。



我看到了两种解决方法,这两种方法对我来说都很难看。


  1. 为嵌套条件单独测试每个属性

  2. 将行包含在try / catch块中以捕获错误并忽略它

我更喜欢PHP的isset()函数,不管抛出什么错误你喂它 - 它只是告诉你,你正在寻找的特定变量是否存在。所以,就像

  //写下从每个TPS报告中出售的单位:
for(i in tpsReports){
if(isset(tpsReports [i] .personnel_info.productivity.units_sold)){
if(tpsReports [i] .personnel_info.productivity.units_sold< 10){
fireEmployee();
}
}
}

有什么想法?

解决方案

 函数isset(obj,propStr){
var parts = propStr.split );
var cur = obj;
for(var i = 0; i< parts.length; i ++){
if(!cur [parts [i]])
return false;
cur = cur [parts [i]];
}
返回true;

$ / code>

注意第二个参数是一个字符串,所以异常没有得到在访问不存在的属性上的属性时抛出。


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

  1. test for each property separately with nested conditionals
  2. 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?

解决方案

function isset(obj, propStr) {
    var parts = propStr.split(".");
    var cur = obj;
    for (var i=0; i<parts.length; i++) {
        if (!cur[parts[i]])
            return false;
        cur = cur[parts[i]];
    }
    return true;
}

Note that the second parameter is a string, so the exception doesn't get thrown when accessing a property on a nonexistent property.

这篇关于在javascript中,测试属性深深地嵌套在对象图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆