How to check for availability for a nested variable without checking all the preceding variable availability, in ReactNative?

前端 未结 2 642
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 05:20

For example, in iOS Swift, I can do something like this:

if (self.user?.company?.pic?.phoneNumber != null) { doSomething() }

Without the need t

2条回答
  •  遥遥无期
    2021-01-27 05:46

    Currently, optional chaining is a stage 3 draft, and so, you may be able to do it in the future.

    EDIT: Optional chaining will now be part of ES2020, and so you'll be able to do the following:

    if (self.user?.company?.pic?.phoneNumber !== undefined) { 
      doSomething(); // phoneNumber exists
    }
    

    With that being said, it still has very limited browser support.

    So, for the time being, you could instead create a function which recursively finds each object from a list of properties like so:

    const optional_chain = (obj, [key, ...props]) =>
      obj !== undefined && key ? optional_chain(obj[key], props) : obj;
    
    const user = {
      company: {
        pic: {
          phoneNumber: 1
        }
      }
    }
    
    console.log(optional_chain(user, ['company', 'pic', 'phoneNumber'])); // 1
    console.log(optional_chain(user, ['company', 'pic', 'phoneNumber', 'x'])); // undefined
    console.log(optional_chain(user, ['company', 'picture', 'phoneNumber'])); // undefined
    console.log(optional_chain(user, ['x', 'picture', 'phoneNumber'])); // undefined

    In your case, the usage would be as so:

    if (optional_chain(self.user, ['company', 'pic', 'phoneNumber']) !== undefined) { 
      doSomething(); 
    }
    

提交回复
热议问题