JavaScript Possible Iteration Over Unexpected

Javascript

Javascript Problem Overview


I have the following code:

  for (i in awards) {
         if (awards[i] instanceof Array === false) {
               console.log(awards[i]);
                httpFactory.patch(awards[i], {"read": true}, false);
             }
       }

My IDE shows this error relating to the code above:

> Possible iteration over unexpected (custom / inherited) members, > probably missing hasOwnProperty check > > Checks for any instances of unfiltered for-in loops in JavaScript. The > use of this construct results in processing inherited or unexpected > properties. You need to filter own properties with hasOwnProperty() > method. The validation works in JavaScript, html or jsp files.

Could you explain in more detail what is meant by this statement?

Javascript Solutions


Solution 1 - Javascript

The IDE is recommending that you add a test:

if (awards.hasOwnProperty(i)) {
    ...
}

inside the for loop.

I personally recommend not doing this, and disabling the warning if possible. There's simply no need in most code, and even less need in ES5 code where you can safely add non-enumerable properties to an object using Object.defineProperty

The hasOwnProperty check is only necessary if you have unsafely added new (enumerable) properties to Object.prototype, so the simplest fix is don't do that.

jQuery doesn't perform this test - they explicitly document that jQuery will break if Object.prototype is unsafely modified.

Solution 2 - Javascript

Every object in javascript has prototype which has its own properties(native/inherited methods/properties) and properties which are directly attached to object itself.

When you iterate over an object, it will iterate the properties of the object itself and the properties of the prototype of the object.

So, In order to avoid iterating over the prototype, it is recommended to use hasOwnProperty method which return true only when the object has the mentioned property directly. i.e, Not inside prototype

Example

for (var k in object) {
  if (object.hasOwnProperty(k)) {
     // do your computation here.
  }
}

More details can be found here

Solution 3 - Javascript

You can also refactor your loop into:

const keys = Object.keys(object);
for (const key of keys){
   // do something with object[key];
}

Solution 4 - Javascript

Also you can get rid of the warning writing a forEach loop for a more readable and functional approach:

Object.keys(object).forEach(key => {
    // Do something with object[key]
});

Solution 5 - Javascript

you should add one more condition at the beginning of this loop

if (awards.hasOwnProperty(i)) 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrometheusView Question on Stackoverflow
Solution 1 - JavascriptAlnitakView Answer on Stackoverflow
Solution 2 - JavascriptSelvaraj M AView Answer on Stackoverflow
Solution 3 - JavascriptFlavien VolkenView Answer on Stackoverflow
Solution 4 - Javascriptjnd0View Answer on Stackoverflow
Solution 5 - JavascriptbmazurekView Answer on Stackoverflow