Mozilla's new Array methods
Just caught this post over at Erik’s regarding the new additions to the Array
object available in Firefox 1.1. The Mozilla documentation explains the new methods which many developers have probably been coding themselves for many years. At risk of repeating Erik, the new methods are:
indexOf()
– bought freakin’ time. I’ve been coding this thing for years now. As you may have guessed, it returns the first index of the object in the array or -1 if not found.lastIndexOf()
– same as above except returns the last index of the object in the array.every()
– takes in a function and runs it on every value in the array. If the function returns true for all values, then it returns true, otherwise it returns false (think of it like an AND operation).some()
– takes in a function and runs it on every value in the array. If the function returns true for any value, then it returns true, otherwise it returns false (think of it like an OR operation).filter()
– takes in a function and returns a new array containing all items that cause the function to return a value of true.forEach()
– takes in a function and runs it on every item in the array (i.e., no morefor
loops needed).map()
– takes in a function, runs it on each item in the array, and returns an array with the results.
As you can tell, most of these have to do with applying a function to every item in the array to some end. This is really a popular technique in the “real world,” and I’m surprised it took people so long to make it native. Can’t wait to see what other goodies Firefox 1.1 will bring.