Archive for December, 2007

Javascript Splice Ain’t Remove

December 27, 2007

I wanted to remove an item from a Javascript array and looked in vain for a remove function. I did find an implementation of remove using the Array.prototype but I just needed a function to get rid of items that matched a specific criterion.

That’s when the trouble started. The splice function—calling its array.splice(index, count) variant—will indeed remove an item or items from the array (and return an array of the items removed for some reason) but it also closes the void left by the removed item and resizes the array all in one fell swoop. So code like this doesn’t work as you’d think:

for (var i = 0; i < array.length; i++)
{
   if (array[i].someParameter == "someValue")
   {
      array.splice(i, 1);
   }
}

Since array.length changes with each removal, this code will end up missing some items because it will end too quickly. In the interest of saving future Googling, frustrated Javascripters, here’s how I solved the issue:

for (var i = array.length - 1; i >= 0; i--)
{
   if (array[i].someParameter == "someValue")
   {
      array.splice(i, 1);
   }
}

By starting the loop from the top of the array and working backwards, item removals and the dynamic resizing have no effect. If anyone has a better way, I’m all ears. Well, eyes.

Advertisements

Review of High School Musical

December 24, 2007

Frequently, I find myself shaking my head at kids today. I’m not quite to the point of requesting them to stay off my yard, but it’s getting closer and closer. The females dress more and more risqué, the males more punky. The music seems insipid, though I know that that’s always been the case. In my more reasonable moments, I consider that there have always been elements of banality and senselessness in every generation.

But then I see a movie like High School Musical and I think that maybe things are getting worse. It makes Mean Girls look like high culture. It trades in every stereotype in the book and wraps its plot (such as it is) in tired bromides. And it is totally lacking in character development. The leads are a consummate bookworm who can’t be bothered to put the book down during a New Year’s Eve party and a basketball jock who spends his whole vacation practicing.

But then they’re paired at the party for a random karaoke and suddenly it’s as if they’ve been on the Mickey Mouse Club their entire childhoods. They don’t need the karaoke machine, their voices are pitch perfect, and they riff off each other as if they’d practiced. But alas their vacations are concluding and they’ll likely never see each other again. Except, lo and behold, she’s just transferred to his school in Albuquerque!

I won’t spoil the rest of the movie because a) I kind of have already forgot a lot of it and b) you shouldn’t really care. I generally agree with Kathy Sierra’s admonition but there’s only so much you can take. I think it’s safe to say that HSM is the Bill & Ted’s Excellent Adventure of this generation.