Skip to content

Instantly share code, notes, and snippets.

@jarrettmeyer
Created September 14, 2010 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarrettmeyer/578843 to your computer and use it in GitHub Desktop.
Save jarrettmeyer/578843 to your computer and use it in GitHub Desktop.
// Because I am an idiot and keep on using 'append', instead of 'push'.
Array.prototype.append = function (item) {
this.push(item);
return this;
};
// Returns true if the given item is contained in the array.
Array.prototype.contains = function (item) {
for (var i = 0; i < this.length; i++) {
if (this[i] == item) return true;
}
return false;
};
// Perform the given action on each element of the array.
Array.prototype.each = function (action) {
for (var i = 0; i < this.length; i++) {
action(this[i]);
}
};
// Remove all occurrences of the given item from an array.
Array.prototype.remove = function (item) {
var i = 0;
while (i < this.length) {
if (this[i] == item) {
this.splice(i, 1);
} else {
i++;
}
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment