How to avoid 'cannot read property of undefined' errors?

前端 未结 16 2519
野性不改
野性不改 2020-11-22 06:02

In my code, I deal with an array that has some entries with many objects nested inside one another, where as some do not. It looks something like the following:



        
16条回答
  •  佛祖请我去吃肉
    2020-11-22 06:21

    If you use Babel, you can already use the optional chaining syntax with @babel/plugin-proposal-optional-chaining Babel plugin. This would allow you to replace this:

    console.log(a && a.b && a.b.c);
    

    with this:

    console.log(a?.b?.c);
    

提交回复
热议问题