Showing posts from October, 2015

How to Filter Out Objects With Empty Child Arrays When Using Angular's ngRepeat

Recently I was working on a website using Angular.js and needed to make use of the ngRepeat directive to iterate through a collection of objects. The UI in question needed to allow the user to select a child object, contained in an array, from one of the objects being displayed. And to provide a better user experience, I decided to only show those objects where the aforementioned array contained at least one object to select. Here's how I accomplished this using Angular's filtering capabilities, where elements is the name of the property containing the child objects to choose from:

<div ng-repeat="section in allSections | filter:{elements: []}:false">

Hopefully this will help someone trying to do something similar. :)

Beware JavaScript's Boolean() Function

I recently wrote the following line of code:

newsletterPopupEnabled = Boolean($("#newsletterPopupEnabled").val());

The point of this was to correctly parse a string of "true" or "false", as well as to take into account an empty string (which I expected to be interpreted as false).

I was very wrong.

The Boolean() function does not parse a string, but simply tests the truthiness of the parameter passed to it. So, a string of "false" is interpreted as truthy because the string is not null.

I now go to ponder the true meaning of "truth"... :)