I need to check whether the status is approved or not, so i check it if it is empty. Whats the most efficient way to do this?
RESPONSE
{
"id": 2,
"email": "yeah@yahoo.com",
"approved": {
"approved_at": "2020"
},
"verified": {
"verified_at": "2020"
}
}
CODE
const checkIfEmpty = (user) => {
if (Object.entries(user.verified).length === 0) {
return true;
}
return false;
};
You can do this way
const checkIfVerifiedExists = (user) => {
if (user && user.verified && Object.keys(user.verified).length) {
return true;
}
return false;
};
console.log(checkIfVerifiedExists(null));
console.log(checkIfVerifiedExists({something: "a"}));
console.log(checkIfVerifiedExists({verified: null}));
console.log(checkIfVerifiedExists({verified: ""}));
console.log(checkIfVerifiedExists({verified: "a"}));
console.log(checkIfVerifiedExists({verified: "a", something: "b"}));
Or More simple You can use Ternary Operator
const checkIfVerifiedExists = (user) => {
return (user && user.verified && Object.keys(user.verified).length) ? true : false
};
console.log(checkIfVerifiedExists(null));
console.log(checkIfVerifiedExists({something: "a"}));
console.log(checkIfVerifiedExists({verified: null}));
console.log(checkIfVerifiedExists({verified: ""}));
console.log(checkIfVerifiedExists({verified: "a"}));
console.log(checkIfVerifiedExists({verified: "a", something: "b"}));