简体   繁体   English

安全访问嵌套 javascript 对象内的属性

[英]Safely access a property inside a nested javascript object

The object structure is as follows.对象结构如下。

object = {
  obj1: {
    obj2: {
       name: 'MY name'
    }
  }
}

This structure is dynamic and sometimes there wont be an obj1.这种结构是动态的,有时不会有 obj1。

So, In react you will be writing the code like..因此,在反应中,您将编写代码,例如..

object && obj1 && obj2 && obj2.name

so that only if object, obj1, and obj2 are present then obj2.name will be displayed.这样只有当存在对象、obj1 和 obj2 时,才会显示 obj2.name。

In this case there wont be any undefined error since the presence of each object is checked prior to going inside the function.在这种情况下,不会有任何未定义的错误,因为在进入函数之前检查每个对象的存在。

Is there alternate way to the above code so that it displays the name when all the objects are present.是否有上述代码的替代方法,以便在所有对象都存在时显示名称。 If not, it should not throw an error.如果没有,它不应该抛出错误。

check like 检查像

object && object.obj1 && object.obj1.obj2 && object.obj1.obj2.name

 let object = { obj1: { } } if(object && object.obj1 && object.obj1.obj2 && object.obj1.obj2.name) { console.log(object.obj1.obj2.name); } else console.log("no obj2.name and no error"); 

Use hasOwnProperty to check if obj1 is present or not 使用hasOwnProperty检查obj1是否存在

 object = { obj1: { obj2: { name: 'MY name' } } } if(object.hasOwnProperty('obj1')) console.log(object.obj1.obj2.name) else console.log(object.obj2.name) 

Try 尝试

 object = { obj1: { obj2: { name: 'MY name' } } } var name = (object.hasOwnProperty('obj1')) ? object.obj1.obj2.name : object.obj2.name; console.log(name); 

I would recommend writing a recursive function that inspects the object and recurses down its children if a name is found. 我建议编写一个递归函数,该函数检查对象并在找到名称后递归其子对象。 I see others suggested catching the error- I would advise against abusing exception handling for runtime errors like this. 我看到其他人建议捕获该错误-我建议不要针对此类运行时错误滥用异常处理。

You need to check every property before reading a nested one: 在读取嵌套属性之前,您需要检查每个属性:

const name = object && object.obj1 && object.obj1.obj2 && object.obj1.obj2.name;

This is laborious and verbose as you have to repeat the same thing over and over to access deeply nested properties. 这很麻烦且冗长,因为您必须一遍又一遍地重复同一件事来访问深层嵌套的属性。

I suggest to use a safeEval function that wraps your potentialy dangerous property access code with a try/catch and returns undefined if an error occurs. 我建议使用safeEval函数,该函数通过try / catch来包装潜在的危险属性访问代码,如果发生错误,则返回undefined This is much shorter than manually checking every property: 这比手动检查每个属性要短得多:

const name = safeEval(() => obj.obj1.obj2.name);

Here is an example: 这是一个例子:

 const obj = { obj1: { obj2: { name: 'Hello' } } } function safeEval(fn) { try { return fn(); } catch { return undefined; } } const a = obj && obj.obj1 && obj.obj1.obj2 && obj.obj1.obj2.name; const b = safeEval(() => obj.obj1.obj2.name); const c = safeEval(() => obj.obj1.obj2.obj3.obj4.name); console.log(a, b, c); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM