Array Functions for JavaScript

I was going to go through the exercise of doing this myself, but it seems that someone already beat me to it. That’s okay — I’ll stand on the shoulders of giants.

These methods first check to make sure that they do not already exist on the Array object. If they do, they don’t overwrite it. If not, they create it.

So that allows you to do this in Firefox and IE:

myarray.forEach( function(item){
   // item is in myarray
   // so do something with it.

});

You also have filter, which is quite handy, too:

function isHidden(divname, index, array) {
   var div = document.getElementsByTagName(divname);
   return (div.style.display=='none');

}
hidden = mydivs.filter( isHidden);

This works exactly like you would expect it to if you have arrays generated by JSON — in other words, like a charm.

Leave a Reply

You must be logged in to post a comment.