Other programming languages have more comprehensive Array methods. While some are rarely used, some are quite useful, and some are merely shortcuts. I have included them all here in my Array Empowerment script.
If anyone can speed any of these up, please let me know. Some of these for loops have to increment, though, as order is important. // Array Empowerment // Remove all elements from Array Array.prototype.clear = function() { this.length = 0; }; // Invoking block once for every element, passing each element as a parameter to block // The result of block is used as the given element in the array Array.prototype.collect = function(block) { var n = this.length; for (var a = 0; a < n; a++) { this[a] = block(this[a]); } }; // Removes all null and undefined elements from Array Array.prototype.compact = function() { this.delete_all(null); this.delete_all(undefined); }; // Deletes items from the Array that are equal to obj Array.prototype.delete_all = function(obj) { var i = -1; var a = this.length; while (--a -(-1)) { if (this[a] == obj) { i = a; break; } else if (this[a] instanceof Array && obj instanceof Array) { if (this[a].eql(obj)) i = a; break; } } if (i > -1) { this.splice(i, 1); this.delete_all(obj); } }; // Deletes the element at the specified index, returning that element, or undefined if the index is out of range Array.prototype.delete_at = function(i) { if (i < this.length && i > -1) return this.splice(i, 1)[0]; return undefined; }; // Deletes every element of Array for which block evaluates to true Array.prototype.delete_if = function(block) { var r = []; var n = this.length; for (var a = 0; a < n; a++) { if (!block(this[a])) { r.push(this[a]); } } this = r; }; // Calls block once for each element in Array, passing that element as a parameter Array.prototype.each = function(block) { var n = this.length; for (var a = 0; a < n; a++) { block(this[a]); } }; // Same as Array.each, but passes the index of the element instead of the element itself Array.prototype.each_index = function(block) { var n = this.length; for (var a = 0; a < n; a++) { block(a); } }; // An array is equal to another array if the lengths are equal and each corresponding element is equal Array.prototype.eql = function(arr) { if (arr.length != this.length) { return false; } else { var a = this.length; while (--a -(-1)) { if (this[a] instanceof Array && arr[a] instanceof Array) { if (!this[a].eql(arr[a])) return false; } else if (this[a] != arr[a]) { return false; } } } return true; }; // Runs block on every item in the array // Returns true if the function returns true for every item Array.prototype.every = function(block) { var a = this.length; while (--a -(-1)) { if (!block(this[a])) return false; } return true; }; // Sets the selected elements of Array (which may be the entire array) to obj // A start of undefined is equivalent to zero. // A length of undefined is equivalent to Array.length. Array.prototype.fill = function(obj, start, len) { if (!start) start = 0; if (len == undefined) len = this.length; len = start + len; if (len > this.length) len = this.length; while (--len - (-1) > start) { this[len] = obj; } }; // Runs a function on every item in the array // Returns an array of all items for which the function returns true. Array.prototype.filter = function(block) { var r = []; var n = this.length; for (var a = 0; a < n; a++) { if (block(this[a])) r.push(this[a]); } return r; }; // Returns a new array that is a one-dimensional flattening of this Array (recursively) // That is, for every element that is an array, extract its elements into the new array Array.prototype.flatten = function(r) { if (!r) r = []; var l = this.length; for (var a = 0; a < l; a++) { if (!(this[a] instanceof Array)) { r.push(this[a]); } else { this[a].flatten(r); } } return r; }; // Returns true if the given obj is present in Array (if any element == obj) Array.prototype.include = function(obj) { var a = this.length; while (--a -(-1)) { if (this[a] == obj) return true; if (this[a] instanceof Array && obj instanceof Array) { if (this[a].eql(obj)) return true; } } return false; }; // Returns the index of the first object in Array such that the object == obj. // Returns undefined if no match is found Array.prototype.index = function(obj) { var a = this.length; while (--a -(-1)) { if (this[a] == obj) return a; if (this[a] instanceof Array && obj instanceof Array) { if (this[a].eql(obj)) return a; } } return undefined; }; // Returns a new array consisting of elements at the given indices. // May insert undefined for indices out of range. Array.prototype.indexes = function() { var r = []; var l = arguments.length; for (var a = 0; a < l; a++) { if (arguments[a] > -1 && arguments[a] < this.length) { r.push(this[arguments[a]]); } else { r.push(undefined); } } return r; }; // Synonym for Array.indexes Array.prototype.indices = Array.prototype.indexes; // // Returns the last element of Array. If the array is empty, returns undefined Array.prototype.last = function() { if (this.length > 0) return this[this.length - 1]; return undefined; }; // Runs a function on every item in the array and returns the results in an array Array.prototype.map = function(block) { var r = []; var n = this.length; for (var a = 0; a < n; a++) { r.push(block(this[a])); } return r; }; // Returns the number of non-null/undefined elements in Array. May be zero Array.prototype.nitems = function() { var c = 0; var a = this.length; while (--a -(-1)) { if (this[a] != null && this[a] != undefined) c++; } return c; }; // Same as Array.each , but traverses Array in reverse order Array.prototype.reverse_each = function(block) { var n = this.length; while(--n -(-1)) { block(this[n]); } }; // Returns the index of the last object in Array such that the object == obj // Returns undefined if no match is found. Array.prototype.rindex = function(obj) { var a = this.length; while (--a -(-1)) { if (!(this[a] instanceof Array && obj instanceof Array)) { if (this[a] == obj) return a; } else { if (this[a].eql(obj)) return a; } } return undefined; }; // Runs a function on every item in the array // Returns true if the function returns true for any one item Array.prototype.some = function(block) { var a = this.length; while (--a -(-1)) { if (block(this[a])) return true; } return false; }; // Returns a new array by removing duplicate values in Array. Array.prototype.uniq = function() { var r = []; var l = this.length; for (var a = 0; a < l; a++) { if (!r.include(this[a])) r.push(this[a]); } return r; }; // MATH METHODS Array.prototype.sum = function() { var x = 0; var a = this.length; while (--a -(-1)) { if (this[a] instanceof Number) { x += this[a]; } else { return undefined; } } }; Array.prototype.mean = function() { var s = this.sum(); if (s != undefined) return this.sum() / this.length; return undefined; }; Array.prototype.min = function() { var a = this.length; if (!a) return undefined; var x = this.last(); while (--a -(-1)) { x = Math.min(this[a], x); } return x; }; Array.prototype.max = function() { var a = this.length; if (!a) return undefined; var x = this.last(); while (--a -(-1)) { x = Math.max(this[a], x); } return x; }; _______________________________________________ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com