Js Prototype number combination and delete duplicate part, js Array
Function DiffArray (a, B ){
This. a =;
This. B = B;
}
DiffArray. prototype. diff = function (){
Var c = [];
Var tmp = this. a. concat (this. B );
Var o = {};
For (var I = 0; I <tmp. length; I ++) (tmp [I] in o )? O [tmp [I] ++: o [tmp [I] = 1;
For (x in o) if (o [x] = 1) c. push (x );
Return c;
};
// Usage
Var a = ['A', 'B', 'C', 'D', 'E'];
Var B = ['D', 'E', 'F', 'G', 'H'];
Var point = new DiffArray (a, B );
Var c = point. diff ();
Alert (c );
// Result
A, B, c, f, g, h
How does js merge two arrays and delete repeated items?
Is it true?
Union
What does that mean?
The array can contain messy elements. The Code is as follows:
// Calculate the union of two Arrays
$. _ CombineTwoArrays = function (first, second ){
If (! $. IsArray (first) |! $. IsArray (second )){
Throw new Error ('$. _ combineTwoArrays function must be set two Array type params ');
}
Var merged = first. concat (second), tlen = merged. length, combined = [];
For (var I = 0; I <tlen; I ++ ){
Var elem = merged. shift (), flag = false;
If (I = 0)
Combined. push (elem );
For (var j = 0; j <combined. length; j ++ ){
Var c = combined [j];
If ($. isArray (elem )){
If (! $. IsArray (c )){
Flag = true;
}
Else if (! $. _ IsTheSameArrays (elem, c )){
Flag = true;
} Else {
Flag = false;
Break;
}
} Else if ($. type (elem) === 'object '){
If (! $. _ IsTheSameObjects (elem, c )){
Flag = true;
} Else {
Flag = false;
Break;
}
} Else {
If (c! = Elem ){
Flag = true;
} Else {
Flag = false;
Break;
}
}
}
If (flag)
Combined. push (elem );
}
Return combined;
};
... Remaining full text>
Javascript defines a function (removing repeated elements of an array) in the prototype of an array object, so that all array objects can apply this method.
Bytes
Function box (arr) {this. arr = arr ;}
Box. prototype. aa = function (){
This. ori = []. concat (this. arr );
For (var I = 0; I <this. arr. length; I ++)
{
For (var j = I + 1; j <this. arr. length; j ++)
{
If (this. arr [I] = this. arr [j])
{
This. arr. splice (j, 1 );
}
}
}
Alert ("the original is:" + this. ori + "\ n" + "after deduplication:" + this. arr );
}
Var num = new box ([,]);
Num. aa ();