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.
1 Answer 1
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
-
\$\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– dev2021年03月25日 19:03:29 +00:00Commented 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– dev2021年03月25日 19:12:49 +00:00Commented 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\$Lucas Wauke– Lucas Wauke2021年03月26日 13:20:04 +00:00Commented Mar 26, 2021 at 13:20
USERS_LIST.find()
is ultimately used, as well as definingcallback
andcheckEligible
. Please read this meta post for more information. \$\endgroup\$