In the following code, the actual length of user.roles is 1. However, the loop runs twice.
When I print the value of i, it displays as "diff" for the second iteration. Switching to the usual solution to the situation for the cycle. However, I would like to know what the problem 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: the user is an object, and the roles are an array of objects. An example of the roles that caused the problem 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 }, ] } ], }