Finding the right property value from array of objects I have a below snippet, here I am using array.find to find the first success case and end it. So here is the code snippet
const USERS_LIST = [
  {
    key: &quot;john&quot;,
    location: &quot;dublin&quot;,
  },
  {
    key: &quot;joe&quot;,
    subKeys: [
      {
        key: &quot;jane&quot;,
        locaction: &quot;washington&quot;,
      },
      {
        key: &quot;mary&quot;,
        location: &quot;newyork&quot;,
      },
    ],
  },
  {
    key: &quot;chris&quot;,
    location: &quot;amsterdam&quot;,
  },
];

so what I am trying here is I go through each object in USERS_LIST and check for a subKeys property. If a subKeys property is present, loop through that and check whether that key is eligible. If it's eligible then I need to stop it or it will pass to the next subkey. If I don't find any subKeys eligible then iterate to the next USERS_LIST object then it takes the key and checks for eligible, whichever I find the first I do a callback and stop executing.
I am able to solve this with this snippet
USERS_LIST.find((user) =&gt; {
      if (user?.subKeys) {
        user.subKeys.find((subUser) =&gt; {
          let subUserStatus = checkEligible({
            action: subUser.key,
          });
          if (subUserStatus) {
            callback(subUser.location);
            return true;
          }
          return false;
        });
      } else {
        let userAccess = checkEligible({ action: user.key });
        if (userAccess) {
          callback(user.location);
          return true;
        }
      }
      return false;
    });
    

Is there a better way using with array.some or any other functions where I can achieve the same output.
Another way to write the same thing is:
const findUser = list =&gt; list.find(u =&gt; {
  if(u.subKeys) {
    return findUser(u.subKeys);
  }
  if(checkEligible({action: u.key}) {
    callback(u);
    return true; // necessary to stop the loop
  }
});

findUser(USERS_LIST);

Once that you only need to receive a list, check if is eligible and do something with this, you can use recursion to avoid the callback repeating in user.subKeys.find and USERS_LIST.find.
Also, the return false is not necessary because the iteration of Array.prototype.find will only stop when there is a return value equivalent to true and a function without a explicit return statement will have the implicit return value of undefined:
const noop = () =&gt; {};
console.log(noop()) // undefined