...

Array Functionality

liorean
05-14-2003, 09:52 PM
An old script I recently found reasons to bring to life again...
/*\
* Array Functionality
* script: array.js
* for: browsers not implementing the full array functionality of ECMA262-3
* by: liorean <http://liorean.web-graphics.com/>
* thanks to: beetle
\*/

if(typeof Array.prototype.copy=='undefined')
Array.prototype.copy=function(a){
var
i=0,
b=[];
for(i;i<this.length;i++)
b[i]=(typeof this[i].copy!='undefined')?
this[i].copy():
this[i];
return b
};

if(typeof Array.prototype.concat=='undefined')
Array.prototype.concat=function(a){
var
i=0,
b=this.copy();
for(i;i<a.length;i++)
b[b.length]=a[i];
return b
};

if(typeof Array.prototype.pop=='undefined')
Array.prototype.pop=function(){
var
b=this[this.length-1];
this.length--;
return b
};

if(typeof Array.prototype.push=='undefined')
Array.prototype.push=function(){
var
i=0,
b=this.length,
a=arguments;
for(i;i<a.length;i++)
this[b+i]=a[i];
return this.length
};

if(typeof Array.prototype.shift=='undefined')
Array.prototype.shift=function(){
var
i=0,
b=this[0];
for(i;i<this.length-1;i++)
this[i]=this[i+1];
this.length--;
return b
};

if(typeof Array.prototype.slice=='undefined')
Array.prototype.slice=function(a,c){
var
i=0,
b,
d=[];
if(!c)
c=this.length;
if(c<0)
c=this.length+c;
if(a<0)
a=this.length-a;
if(c<a){
b=a;
a=c;
c=b
}
for(i;i<c-a;i++)
d[i]=this[a+i];
return d
};

if(typeof Array.prototype.splice=='undefined')
Array.prototype.splice=function(a,c){
var
i=0,
e=arguments,
d=this.copy(),
f=a;
if(!c)
c=this.length-a;
for(i;i<e.length-2;i++)
this[a+i]=e[i+2];
for(a;a<this.length-c;a++)
this[a+e.length-2]=d[a-c];
this.length-=c-e.length+2;
return d.slice(f,f+c)
};

if(typeof Array.prototype.unshift=='undefined')
Array.prototype.unshift=function(a){
var
b;
this.reverse();
b=this.push(a);
this.reverse();
return b
};

beetle
05-20-2003, 05:19 AM
Nice :D

I'd probably want the copy() method to handle multiple dimensional arrays :thumbsup:

liorean
05-20-2003, 04:07 PM
Sorry, but the only way to do that requires ECMA262-3 compliancy. This means older browasers like ie5.0 are screwed. (If I remember correctly, instanceof operator is an ECMA262-3 feature.)

beetle
05-20-2003, 04:28 PM
What's wrong with doing it this way?
Array.prototype.copy = function()
{
var
i=0,
b=[];
for(i;i<this.length;i++) {
b[i] = ( typeof this[i].copy != 'undefined' )?
this[i].copy():
this[i];
}
return b;
};Besides, according to this page (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoriversioninformation.asp), instanceof was introduced in IE5.0 (JScript5.0) -- and this page (http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ops.html#1055015) says it was implemented in Javascript 1.4 - but don't get me lying as to which browsers support 1.4.

liorean
05-20-2003, 05:02 PM
Oh, great, more version confusion (See my discussion with Brothercake regarding the in operator in <http://codingforums.com/showthread.php?s=&threadid=16119&highlight=implementation+in+document>). In fact there's no browser supporting JavaScript 1.4, only the ONE server platform. NN4 used JavaScript 1.2 and 1.3, never 1.4. Oh, and the Sourceforge project that is based on the Netscape JavaScript engine, whatever it's name is.

Mozilla has a slightly rewritten version of the 1.4 engine that is numbered 1.5.

I'm a bit surprised that IE5 has it, but then again, Microsoft was beginning to gain on Netscape at that time.



Well, your version has better support anyway. I'll update the script.

(Lovely use of recursion, even if it isn't tail recursion, by the way.)

beetle
05-20-2003, 05:27 PM
Thanks ;)

Vladdy
05-21-2003, 01:51 AM
Here are two methods I had to add to Array:

Array.prototype.swap = function(index1,index2)
{ var temp = this[index1];
this[index1] = this[index2];
this[index2] = temp;
return;
}

Array.prototype.shuffle = function()
{ for(var i=0; i<this.length; i++)
{ ind1 = Math.floor(Math.random()*this.length);
ind2 = Math.floor(Math.random()*this.length);
this.swap(ind1,ind2);
}
return;
}

Choopernickel
06-12-2003, 04:53 PM
My Array extensions:
Array.prototype.insert = function(newValue,position) {
if (position && position>-1)
this.splice(position,0,newValue);
else
this.push(newValue);
}

Array.prototype.insertBefore = function(newValue,beforeWhat) {
targetIndex = this.locate(beforeWhat);
if (targetIndex == -1)
this.push(newValue);
else
this.insert(newValue,targetIndex);
}

Array.prototype.insertAfter = function(newValue,afterWhat) {
targetIndex = this.locate(afterWhat);
if (targetIndex == -1)
this.push(newValue);
else
this.insert(newValue,targetIndex+1);
}

Array.prototype.dele = function(toDelete) {
var tmp = this.splice(this.locate(toDelete),1);
}

Array.prototype.sum = function() {
var i,mySum=0;
for (i=this.length; i>0; i--)
mySum += parseInt(this[i-1],10);
return mySum;
}

Array.prototype.avg = function() {
var s = this.sum();
return s/this.length;
}

Array.prototype.max = function() {
var m = -1;
if (typeof this[0] == "number") m = this[0];
for (i=this.length; i>0; i--) {
if (typeof this[i] == "number")
m = Math.max(m,this[i]);
}
return m;
}

Array.prototype.min = function() {
var m = -1;
if (typeof this[0] == "number") m = this[0];
for (i=this.length; i>0; i--) {
if (typeof this[i] == "number")
m = Math.min(m,this[i]);
}
return m;
}

Array.prototype.search = function(searchFor) {
var i=0;
var j=0;
var results=Array(0);
var start = 0;
found = -1;
for (i=0; i<this.length; i++) {
found = this.locate(searchFor,start);
if (found>-1) {
results.push(this[found]);
start = found+1;
}
}
return results;
}

Array.prototype.locate = function(what, start){
var f = 0;
if (start)
startWhere = start
else
startWhere = 0;
for(f=startWhere; f<this.length; f++){
if(this[f].toString().substr(0,what.length) == what.toString())
return f;
}
return -1;
}

Skyzyx
06-13-2003, 07:06 AM
Opera 6 and 7 support JavaScript 1.4... in case anyone cares. :)

brothercake
06-13-2003, 09:48 AM
opera 6 only supports a limited subset

liorean
11-24-2003, 05:14 PM
Originally posted by Skyzyx
Opera 6 and 7 support JavaScript 1.4... in case anyone cares. :) Not really. They support about the same level of JavaScript as 1.4, but it's only their own approximation. I know a few details of 1.4 that no browser today supports.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum