2
\$\begingroup\$

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: "john",
    location: "dublin",
  },
  {
    key: "joe",
    subKeys: [
      {
        key: "jane",
        locaction: "washington",
      },
      {
        key: "mary",
        location: "newyork",
      },
    ],
  },
  {
    key: "chris",
    location: "amsterdam",
  },
];

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) => {
      if (user?.subKeys) {
        user.subKeys.find((subUser) => {
          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.

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Review! To give reviewers a better picture of what the code does please describe how the result of calling USERS_LIST.find() is ultimately used, as well as defining callback and checkEligible. Please read this meta post for more information. \$\endgroup\$ Mar 25, 2021 at 14:26
  • \$\begingroup\$ Basically need to find the first key which do some check in checkEligible, basically checkEligible returns either true or false, and callback is a function to execute some other code snippets, so here should i just replace .find with .some is thats the only change if i use array.some method any other changes required, also to achieve the same is there any better way @SᴀᴍOnᴇᴌᴀ \$\endgroup\$
    – dev
    Mar 25, 2021 at 14:30

1 Answer 1

1
\$\begingroup\$

Another way to write the same thing is:

const findUser = list => list.find(u => {
  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 = () => {};
console.log(noop()) // undefined
\$\endgroup\$
3
  • \$\begingroup\$ This helped me a lot :) thanks lucas, just one doubt if i use .some or .find both works, so which method i should use, since i thought that since find is defined as to find the first one but some is more of more than one so just a confusion \$\endgroup\$
    – dev
    Mar 25, 2021 at 19:03
  • \$\begingroup\$ If we use .some also internally no need to return false isn't, it will behave the same as like the .find, correct me if am wrong on this. Just curious :) \$\endgroup\$
    – dev
    Mar 25, 2021 at 19:12
  • \$\begingroup\$ No prob, John, I am glad that I could help! About your doubt, I think that in this case there is no difference in use .find or .some. As you said, they will behave the same way, but you can think that the difference is that .some will return a boolean if it finds an element that satisfies the callback return statement and .find will return the first element which satisfies the return statement. So, it depends of what you want to do with the return value of those methods. \$\endgroup\$ Mar 26, 2021 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.