javascript – Getting value from a mixed object array HTTP endpoint


I get location data by a HTTP call. Initially I wrote the following code to check whether data.res.accounts(0).contacts(0).location exists:

const getLocation = async () => {
  const data = await fetchLocation();
  if (
    data == null
    data.res == null ||
    data.res.accounts == null ||
    data.res.accounts(0) == null ||
    data.res.accounts(0).contacts == null ||
    data.res.accounts(0).contacts(0) == null ||
    data.res.accounts(0).contacts(0).location == null
  ) {
    return null;
  }
  return data.res.accounts(0).contacts(0).location;
}

I have tried to simplify the mixed object and array code.
One way of doing this is using JavaScript’s new conditional operator ?..

For example if they are all objects then I can use something like:

if (data?.res?.account?.contact?.location == null) {
  return null;
}

However my data is mixed with arrays and objects. This is the best I’ve come up with:

if (
  data?.res?.accounts == null ||
  data.res.accounts(0)?.contacts == null ||
  data.res.accounts(0).contacts(0)?.location == null
) {
  return null;
}

Can I simplify it further?