Revision 111952 of Array

  • Revision slug: Talk:JavaScript/Reference/Global_Objects/Array
  • Revision title: Array
  • Revision id: 111952
  • Created:
  • Creator: RickMeasham
  • Is current revision? No
  • Comment Argument against multi-demensioned arrays

Revision Content

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)

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)

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)

Revision Source

<p>iterative array methods (also works on strings) library for browsers that support Object.prototype.call:
</p>
<pre>if (!Array.prototype.indexOf)
	Array.prototype.indexOf = function(item, startIndex) {
		var len = this.length;
		if (startIndex == null)
			startIndex = 0;
		else if (startIndex &lt; 0) {
			startIndex += len;
			if (startIndex &lt; 0)
				startIndex = 0;
		}
		for (var i = startIndex; i &lt; len; i++) {
			var val = this[i] || this.charAt &amp;&amp; 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 &gt;= len)
			startIndex = len - 1;
		else if (startIndex &lt; 0)
			startIndex += len;
		for (var i = startIndex; i &gt;= 0; i--) {
			var val = this[i] || this.charAt &amp;&amp; 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 &lt; len; i++)
			func.call(thisVal, this[i] || this.charAt &amp;&amp; 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 &lt; len; i++)
			ret[i] = func.call(thisVal, this[i] || this.charAt &amp;&amp; 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 &lt; len; i++) {
			var val = this[i] || this.charAt &amp;&amp; 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 &lt; len; i++)
			if (!func.call(thisVal, this[i] || this.charAt &amp;&amp; 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 &lt; len; i++)
			if (func.call(thisVal, this[i] || this.charAt &amp;&amp; this.charAt(i), i, this))
				return true;
		return false;
	};
</pre>
<p>--<a href="User:Maian">Maian</a> 20:26, 11 October 2005 (PDT)
</p>
<h3 name="Array_constructor_behavior"> Array constructor behavior </h3>
<p>From the article:
</p>
<dl><dd><i>When you specify a single parameter with the Array constructor, the behavior depends on whether you specify LANGUAGE="JavaScript1.2" in the &lt;SCRIPT&gt; tag:</i>
</dd></dl>
<p>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. <a href="User:AdmiralNovia">AdmiralNovia</a> 11:42, 17 February 2006 (PST)
</p>
<h3 name="N-dimensional_arrays"> N-dimensional arrays </h3>
<p>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.
--<a href="User:RickMeasham">RickMeasham</a> 03:10, 18 February 2008 (PST)
</p>
Revert to this revision