Talk:Core JavaScript 1.5 Reference:Global Objects:Array
From MDC
iterative array methods (also works on strings) library for browsers that support Object.prototype.call:
if (!Array.prototype.indexOf)
Array.prototype.indexOf = function(item, startIndex) {
var len = this.length;
if (startIndex == null)
startIndex = 0;
else if (startIndex < 0) {
startIndex += len;
if (startIndex < 0)
startIndex = 0;
}
for (var i = startIndex; i < len; i++) {
var val = this[i] || this.charAt && this.charAt(i);
if (val == item)
return i;
}
return -1;
};
if (!Array.prototype.lastIndexOf)
Array.prototype.lastIndexOf = function(item, startIndex) {
var len = this.length;
if (startIndex == null || startIndex >= len)
startIndex = len - 1;
else if (startIndex < 0)
startIndex += len;
for (var i = startIndex; i >= 0; i--) {
var val = this[i] || this.charAt && this.charAt(i);
if (val == item)
return i;
}
return -1;
};
if (!Array.prototype.forEach)
Array.prototype.forEach = function(func, thisVal) {
var len = this.length;
for (var i = 0; i < len; i++)
func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this);
};
if (!Array.prototype.map)
Array.prototype.map = function(func, thisVal) {
var len = this.length;
var ret = new Array(len);
for (var i = 0; i < len; i++)
ret[i] = func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this);
return ret;
};
if (!Array.prototype.filter)
Array.prototype.filter = function(func, thisVal) {
var len = this.length;
var ret = new Array();
for (var i = 0; i < len; i++) {
var val = this[i] || this.charAt && this.charAt(i);
if(func.call(thisVal, val, i, this))
ret[ret.length] = val;
}
return ret;
};
if (!Array.prototype.every)
Array.prototype.every = function(func, thisVal) {
var len = this.length;
for (var i = 0; i < len; i++)
if (!func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this))
return false;
return true;
};
if (!Array.prototype.some)
Array.prototype.some = function(func, thisVal) {
var len = this.length;
for (var i = 0; i < len; i++)
if (func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this))
return true;
return false;
};
--Maian 20:26, 11 October 2005 (PDT)
[edit] Array constructor behavior
From the article:
- When you specify a single parameter with the Array constructor, the behavior depends on whether you specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag:
That's great, except that it doesn't tell us what the behavior is outside of web browsers, such as when SpiderMonkey is embedded in another application. AdmiralNovia 11:42, 17 February 2006 (PST)
[edit] N-dimensional arrays
I'd like to argue that we remove (well, reword) this section of the page as JS, like most languages, doesn't have multi-dimensioned arrays, but has arrays-of-arrays. A multi-dimensioned array is rectangular in nature, but a JS array can contain an arbitrary number of elements which can each take an array of arbitrary size, a String, a Number, a Date, or any other Object. --RickMeasham 03:10, 18 February 2008 (PST)