Array.every()###
array.every(f)
; array.every(f, o)
; f(array[i], i, array)
[1,2,3].every(function(x) { return x < 5; }) // => true
[1,2,3].every(function(x) { return x < 3; }) // => false
[].every(function(x) { return false; }); // => true: always true for []
//
if(!Array.prototype.every) {
Array.prototype.every = function (func /*,thisp*/) {
var i = 0,len = this.length >>> 0;
if(typeof func !== 'function') {
throw new TypeError();
}
var thisp = arguments[1];
for(; i < len; ++i) {
if(i in this && !func.call(thisp,this[i],i,this)) {
return false;
}
}
return true;
}
}
Array.some()###
array.some(predicate)
; array.some(predicate, o)
[1,2,3].some(function(x) { return x > 5; }) // => false: no elts are > 5
[1,2,3].some(function(x) { return x > 2; }) // => true: some elts are > 3
[].some(function(x) { return true; }); // => false: always false for []
//
if(!Array.prototype.some) {
Array.prototype.some = function (func /*,thisp*/) {
var i = 0,len = this.length >>> 0;
if(typeof func !== 'function') {
throw new TypeError();
}
var thisp = arguments[1];
for(; i < len; ++i) {
if(i in this && func.call(thisp,this[i],i,this)) {
return true;
}
}
return false;
}
}
Array.filter()
array.filter(f)
; array.filter(f, o)
; f(array[i], i, array)
[1,2,3].filter(function(x) { return x > 1; }); // => [2,3]
//
if(!Array.prototype.filter) {
Array.prototype.filter = function (func /*,thisp*/) {
var len = this.length >>> 0;
if(typeof func !== 'function') {
throw new TypeError();
}
var res = new Array(len);
var thisp = arguments[1];
for(var i = 0; i < len; ++i) {
if(i in this) {
var val = this[i];
if(func.call(thisp,val,i,this)) {
res.push(val);
}
}
}
return res;
}
}
Array.map()
array.map(f)
; array.map(f, o)
; a[i] = f(array[i], i, array)
[1,2,3].map(function(x) { return x * 2; }); // => [2,4,6]
//
if(!Array.prototype.map) {
Array.prototype.map = function (func /*,thisp*/) {
var len = this.length >>> 0;
if(typeof func !== 'function') {
throw new TypeError();
}
var res = new Array(len);
var thisp = arguments[1];
for(var i = 0; i < len; ++i) {
if(i in this) {
res[i] = func.call(thisp,this[i],i,this);
}
}
return res;
}
}
Array.forEach()
array.forEach(f)
; array.forEach(f, o)
; f(array[i], i, array)
var a = [1,2,3];
a.forEach(function(x,i,a) { a[i]++; }); // a is now [2,3,4]
//
if(!Array.prototype.forEach) {
Array.prototype.forEach = function (func /*,thisp*/) {
var len = this.length >>> 0;
if(typeof func !== 'function') {
throw new TypeError();
}
var thisp = arguments[1];
for(var i = 0; i < len; ++i) {
if(i in this) {
func.call(thisp,this[i],i,this);
}
}
}
}
Array.reduce()/ Array.reduceRight()
array.reduce(f)
; array.reduce(f, initial)
[1,2,3,4].reduce(function(x,y) { return x*y; }) // => 24: ((1*2)*3)*4
Array.concat()###
array.concat(value, ...)
var a = [1,2,3];
a.concat(4, 5) // => [1,2,3,4,5]
a.concat([4,5]); // => [1,2,3,4,5]
a.concat([4,5],[6,7]) // => [1,2,3,4,5,6,7]
a.concat(4, [5,[6,7]]) // => [1,2,3,4,5,[6,7]]
Array.indexOf() /Array.lastIndexOf()
array.indexOf(value)
; array.indexOf(value, start)
['a','b','c'].indexOf('b') // => 1
['a','b','c'].indexOf('d') // => -1
['a','b','c'].indexOf('a',1) // => -1
//
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function (elt /*,from*/) {
var len = this.length >>> 0,
from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if(from < 0) {
from += len;
}
for(; from < len; ++from) {
if(from in this && this[from] === elt)
return from;
}
return -1;
}
}
Array.join()###
array.join()
; array.join(separator)
a = new Array(1, 2, 3, "testing");
s = a.join("+"); // s is the string "1+2+3+testing"
Array.slice()###
array.slice(start, end)
var a = [1,2,3,4,5];
a.slice(0,3); // Returns [1,2,3]
a.slice(3); // Returns [4,5]
a.slice(1,-1); // Returns [2,3,4]
a.slice(-3,-2); // Returns [3]; buggy in IE 4: returns [1,2,3]
Array.splice()###
array.splice(start, deleteCount, value, ...)
var a = [1,2,3,4,5,6,7,8]
a.splice(1,2); // Returns [2,3]; a is [1,4]
a.splice(1,1); // Returns [4]; a is [1]
a.splice(1,0,2,3); // Returns []; a is [1 2 3]
Array.reverse()###
array.reverse()
a = new Array(1, 2, 3); // a[0] == 1, a[2] == 3;
a.reverse(); // Now a[0] == 3, a[2] == 1;
Array.sort()###
array.sort()
; array.sort(orderfunc)
// An ordering function for a numerical sort
function numberorder(a, b) { return a - b; }
a = new Array(33, 4, 1111, 222);
a.sort(); // Alphabetical sort: 1111, 222, 33, 4
a.sort(numberorder); // Numerical sort: 4, 33, 222, 1111
Array.push()/ Array.pop()###
array.push(value, ...)
; array.pop()
var stack = []; // stack: []
stack.push(1, 2); // stack: [1,2] Returns 2
stack.pop(); // stack: [1] Returns 2
stack.push([4,5]); // stack: [1,[4,5]] Returns 2
stack.pop() // stack: [1] Returns [4,5]
stack.pop(); // stack: [] Returns 1
Array.shift()###
array.shift()
var a = [1, [2,3], 4]
a.shift(); // Returns 1; a = [[2,3], 4]
a.shift(); // Returns [2,3]; a = [4]
Array.unshift()###
array.unshift(value, ...)
var a = []; // a:[]
a.unshift(1); // a:[1] Returns: 1
a.unshift(22); // a:[22,1] Returns: 2
a.shift(); // a:[1] Returns: 22
a.unshift(33,[4,5]); // a:[33,[4,5],1] Returns: 3