Posts

Showing posts with the label JavaScript

Using Underscore.js To Split an Array By Groups

Image
While working on a project today, I had to split an array of JSON objects into separate arrays based on one of the object's properties. Underscore.js makes this easy. In the example below, I'm splitting the sections  array into a new, multidimensional array called regions . var regions = _.toArray(_.groupBy(sections, function(section) {               return section.region;             })); Here's an example of the output logged to the browser's console:

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"... :)

The "TypeError: n.curCSS is not a function‏" Error

Yesterday at work we deployed a new version of one of our websites to production after a successful test cycle in our QA and staging environments. However, once the site was deployed to production, a script error, "TypeError: n.curCSS is not a function‏", would appear whenever an attempt was made to display a jQuery UI dialog. This left me scratching my head. It had worked fine in QA and staging, and the code in question had been around for months. It definitely had something to do with a difference in the production environment, but what? The error in question was occurring in a JavaScript bundle being created by the website (which is an ASP.NET MVC 4.5 site) on start-up. I could view the source and see the offending line. After Googling the error message, I learned that it was related to an incompatibility between versions of jQuery and jQuery UI -- but again, this had worked perfectly in the other environments, and for quite some time. How could it be an incompatibilit...