In the following code, the actual length of user.roles is 1. However, the loop runs two times.
When I output the value of i, it is shown as 'diff' for the second iteration. Switching to the ordinary for loop resolved the situation. However, I'd like to know what the issue is with the for..in loop.
for (var i in user.roles) {
if (user.roles[i].school.equals(schoolId)) {
for (var j in user.roles[i].permissions) {
for (var k in accessType) {
if (user.roles[i].permissions[j].feature == featureKey) {
if (user.roles[i].permissions[j][accessType[k]]) {
return true;
}
}
}
}
}
}
Update: user is an object, and roles is an array of objects. The instance of roles that caused the issue is shown below:
{
"_id": "582d3390d572d05c1f028f53",
"displayName": "Test Teacher Attendance",
"gender": "Male",
"roles": [
{
"_id": "57a1b3ccc71009c62a48a684",
"school": "57a1b3ccc71009c62a48a682",
"role": "Teacher",
"__v": 0,
"designation": true,
"permissions": [
{
"feature": "User",
"_id": "57ac0b9171b8f0b82befdb7d",
"review": false,
"view": true,
"delete": false,
"edit": false,
"create": false
},
{
"feature": "Notice",
"_id": "57ac0b9171b8f0b82befdb7c",
"review": false,
"view": true,
"delete": false,
"edit": false,
"create": false
},
]
}
],
}