DEV Community

Discussion on: Quick JavaScript Tip: the some() method

Collapse
 
mitchelllogan profile image
Logan Mitchell • Edited

It is quite handy!

I used .some() coupled with .includes() a while back to cross-reference two string arrays to make sure the user had at least one of the required roles for an action. The benefit of .some() here is that it will return true no matter how many of the iterations are false, as long as one of them is true.

//sample data
let userRoles = ['a','b','c','d'];
let validRoles = ['c','d','e','f'];
//end sample data

private isRoleValid(userRoles: string[], validRoles: string[]) {
    return userRoles && userRoles.some(e => validRoles.includes(e));
}