Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an if with a continue a good pattern to prevent excessive nesting while iterating over properties in Javascript?

I normally use this pattern to iterate over object properties:

for(var property in object) {
   if(object.hasOwnProperty(property)) {
      ...
   }
}

I don't like this excessive indentation and recently it was pointed out to me that I could get rid of it by doing this:

for(var property in object) {
   if(!object.hasOwnProperty(property)) {
      continue;
   }

   ...
}

I like this because it doesn't introduce the extra level of indentation. Is this pattern alright, or are there better ways?

like image 242
Vivin Paliath Avatar asked Sep 22 '10 01:09

Vivin Paliath


People also ask

Which JavaScript loop ensures that at least a singular iteration will happen?

while loop, condition is checked at the end of each iteration of the loop, rather than at the beginning before the loop runs. This means that code in a do... while loop is guaranteed to run at least once, even if the condition expression already evaluates to true .

How can you iterate over the properties of a JavaScript object?

If you would like to iterate directly over the values of the keys of an object, you can define an iterator , just like JavaScipts's default iterators for strings, arrays, typed arrays, Map and Set. JS will attempt to iterate via the default iterator property, which must be defined as Symbol. iterator .

Which method will you use to iterate the properties of an object?

The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.


2 Answers

I personally prefer:

for(var property in object) if(object.hasOwnProperty(property)) {
     ...
}

There is no extra level of indentation because for, if, etc. will take the next statement if you leave out the curly braces. Since we put all of our code inside the if hasOwnProperty block it makes any braces for the for statement unnecessary.

Essentially it's equivalent to:

for(var property in object) {
   if(object.hasOwnProperty(property)) {
     ...
   } else {
      continue;
   }
}
like image 63
Cristian Sanchez Avatar answered Oct 14 '22 06:10

Cristian Sanchez


Syntacticly I'd prefer something like this

function own(obj) {
  var ownprops = {};
  for (var prop in obj) 
    if (obj.hasOwnProperty(prop)) ownprops[prop] = 1;

  return ownprops;
}

for (var property in own(object)) {
 //code
}

Looks nice, but it entails two loops over the same object, not pretty performance wise.

The other way to do it is functionaly

function foreach(obj, func, thisp) {
  for (var prop in obj)
    if (obj.hasOwnProperty(prop)) func.call(thisp, obj[prop], prop);
}

foreach(object, function(val, key) {
  //code
});

Only one loop, but a function is called for every iteration, which is not great performance wise but better than the last solution. Note that it also clobbers the value of this, but you can pass that value explicitly as the optional 3rd argument.

Just some alternatives. The way you are doing it, and the way explained by Daniel is just fine, and without performance compromises.

Also, I'd like to point out that you do not have to indent your code for every single curly brace...

like image 45
MooGoo Avatar answered Oct 14 '22 04:10

MooGoo