Follow @endyourif rss Facebook LinkedIn

Using a forEach loop with JavaScript

Published on Apr 7, 2019

I honestly feel like I've been living in the stone age. For years I've always used a standard for loop when iterating a JavaScript array accessing the property with the index of my for loop. No longer, it's time to upgrade (my brain) and use the forEach loop.

Knockout.js foreach afterRender when loop is completed

Published on Apr 3, 2019

I have been using Knockout.js's afterRender for years to hide a progress bar when the foreach loop completed. All of those years I have been using it incorrectly. Once I read Knockout's documentation about the foreach afterRender function, I realized that it is actually called after each render of the element inside the foreach. This is definitely not how I wanted this function to work. In this Knockout.js tutorial let me show you my solution to accomplish applying the afterRender when the foreach has completed.

A Short Rant About Coding Conventions

Published on Sep 13, 2012

Yesterday's article actually got me a little amped up about coding conventions Comparing a while loop against a foreach loop of an array because I never thought I would actually have to do a comparison between a while loop and a foreach loop on an array! If we go back and revisit the post, I was reviewing a recent CakePHP commit for an optimization on the Hash class. The code in question is three separate blocks of code that leverage the array_shift function to get the next value in the array with a foreach loop instead.

Comparing a while loop against a foreach loop of an array

Published on Sep 12, 2012

Are you confused by the title? I was when I first got the idea to even write this blog as well. I was recently perusing the CakePHP change logs and came across an interesting commit Optimization for Hash Method.

The optimization is quite simple, this code:



while (($key = array_shift($parts)) !== null)



Is replaced with the following code:



foreach ($parts as $key)



This is actually done several times in the commit. It seems that the original developer is really keen on using the array_shift function. Just in seeing this code, I thought the original code was odd to utilize that function so I immediately had to do a comparison and validate the optimization!