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
},
]
}
],
}
user.roles
seems to be an array. And for array you should not use for in.
Simple example
var arr = [2];
arr.s = 3;
for (var i in arr) {
console.log("here"); // paints twice
}
From MDN, The for...in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.
How to choose the type of iterator, here is a reference iterators
EDIT
As per the updated question, the above can only come with a property as diff
if somewhere in the code following is present
Array.prototype.diff = .....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With