Check if object member exists in nested object

后端 未结 8 1005
花落未央
花落未央 2020-11-27 05:00

Is there a simpler way than using ___ in object to check the existence of each level of an object to check the existence of a single member?

More conci

8条回答
  •  星月不相逢
    2020-11-27 05:26

    You could also try/catch TypeError?

    try {
      console.log(someObject.member.member.member.value);
    } catch(e) {
      if (e instanceof TypeError) {
        console.log("Couldn't access someObject.member.member.member.value");
        console.log(someObject);
      }
    }
    

提交回复
热议问题