P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Array
:
Array.prototype.merge
author:
demons
[+]
,
Submitted: 12.13.01 2a
// connect 2 array to 1 //from Demon.S function lib Array.prototype.merge=function (arrayA,arrayB,order) { var lengthA=arrayA.length; var lengthB=arrayB.length; var i,j; // connect 2 array for (i=0; i<lengthB; i++) { for (j=0; j<lengthA; j++) { if (arrayB[i] <> arrayA[j]) { continue; } else { break; } } if (j==lengthA) { arrayA.push(arrayB[i]); } } // order if (order) { arrayA.sort(); } // return return arrayA; }
usage
a=[1,2,3,4]; b=[3,4,5,6]; c=Array.merge(a,b,true); reslult [1,2,3,4,5,6]; D:)S
msg
1
{
1stpixel
[+]
, posted: 12.22.01 2p•-, top
[^]
}
sorry ... doesn't work at all ... :-[
msg
2
{
demons
[+]
, posted: 02.06.02 8a•-, top
[^]
}
usage /
a=[1,2,3,4];
b=[3,4,5,6];
c=Array.merge(a,b,true);
reslult [1,2,3,4,5,6];
msg
3
{
orangechicken
[+]
, posted: 11.10.02 6p•-, top
[^]
}
His Usage is incorrect. Because it's a prototype, you have to call it on an array.
Here's a re-written version that works:
Array.prototype.merge=function( a2, o ) { var a1L=this.length; var a2L=a2.length; var i,j; // merge a2 into this for (i=0; i<a2L; i++) { for (j=0; j<a1L; j++) { if (a2[i] <> this[j]) { continue; } else { break; } } if (j==a1L) { this.push(a2[i]); } } // order if(o) { this.sort(); } }
Usage:
a=[1,2,3,4]; b=[3,4,5,6]; a.merge(b,true); trace( a ); // returns: 1, 2, 3, 4, 5, 6 a=[1,2,3,4]; b=[6,5,4,3]; a.merge(b,false); trace( a ); // returns: 1, 2, 3, 4, 6, 5
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@