Js Prototype number combination and delete duplicate part, js Array

Source: Internet
Author: User

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.