On the problem of JS array going heavy _javascript skills

Source: Internet
Author: User

1. according to the JS object key does not repeat the principle, the idea of several groups to weight the method, according to the most conventional thinking as follows:

Copy Code code as follows:

function Distinctarray (arr) {
var obj={},temp=[];
for (Var i=0;i<arr.length;i++) {
if (!obj[arr[i]]) {
Temp.push (Arr[i]);
Obj[arr[i]] =true;
}
}
return temp;
}
var testarr=[1,2,3,2];
Console.log (Distinctarray (Testarr));//[1,2,3]

Looks good, but if it turns out to be a situation:
var testarr1=[1,2,3, "2"];
Console.log (Distinctarray (Testarr));//[1,2,3]
Even the same result, this is not what we want, we need the result should be [1,2,3, "2"]. That is, the need to ensure the integrity of the type in the process of going heavy.

In view of the above, we have improved the above methods:

Copy Code code as follows:

function Distinctarrayimprove (arr) {
var obj={},temp=[];
for (Var i=0;i<arr.length;i++) {
if (!obj[typeof (Arr[i]) +arr[i]]) {
Temp.push (Arr[i]);
Obj[typeof (Arr[i]) +arr[i]] =true;
}
}
return temp;
}

The above method adds the typeof prefix when putting key to the object, then lets see the effect.
var testarr1=[1,2,3, "2"];
Console.log (Distinctarray (Testarr));//[1,2,3, "2"]
Oh, good! So is not this function is completely OK, let us look at a situation!
var testarr1=[1,2,3, "2", {a:1},{b:1}];
Console.log (Distinctarray (Testarr));//[1,2,3, "2", {a:1}]
Unexpectedly appear this result, how the {b:1} to inexplicably deleted it, to heavy process if the deletion of useful data is a serious problem, so the above method is not a perfect, then let us go on to look down.

2. in 1, our main idea is to use JS object in the concept of key does not repeat the idea to guide our thinking, but ultimately did not solve all the problems, then we can consider a different mode of thinking to achieve the function we want.

Using the slice and splice methods to achieve the weight of the array, as follows:

Copy Code code as follows:

function DistinctArray2 (arr) {
var temp=arr.slice (0);//array copy one to Temp
for (Var i=0;i<temp.length;i++) {
for (j=i+1;j<temp.length;j++) {
if (Temp[j]==temp[i]) {
Temp.splice (j,1);//Delete the element
j--;
}
}
}
return temp;
}

Test:
var testarr1=[1,2,3, "2"];
Console.log (Distinctarray (Testarr));//[1,2,3]
var testarr2=[1,2,2,{a:1},{a:1},{a:1,b:2},function () {alert ("B");},function () {alert ("B");}];
[1,2,{a:1},{a:1},{a:1,b:2},function () {alert ("B");},function () {alert ("B");}]

The test results still do not meet our needs. After our team research, we found that the main problem in the comparison of two objects equal to the operation, distinctArray2 use of "= =" to compare, and can not distinguish between the contents of large objects are equal, in view of this situation, we wrote another method:

Copy Code code as follows:

function Distinctarrayall (arr) {
var isequal=function (obj1,obj2) {
Two object addresses are equal and must be equal
if (OBJ1===OBJ2) {
return true;
}
if (typeof (Obj1) ==typeof (obj2)) {
if (typeof (obj1) = = "Object" &&typeof (obj2) = = "Object") {
var pcount=0;
For (var p in obj1) {
pcount++;
if (!isequal (Obj1[p],obj2[p])) {
return false;
}
}
For (var p in Obj2) {
pcount--;
}
return pcount==0;
}else if (typeof (obj1) = = "function" &&typeof (obj2) = = "function") {
if (obj1.tostring ()!=obj2.tostring ()) {
return false;
}
}else {
if (OBJ1!=OBJ2) {
return false;
}
}
}else{
return false;
}
return true;
}
var temp=arr.slice (0);//array copy one to Temp
for (Var i=0;i<temp.length;i++) {
for (j=i+1;j<temp.length;j++) {
if (IsEqual (Temp[j],temp[i])) {
Temp.splice (j,1);//Delete the element
j--;
}
}
}
return temp;
}

Test:
var testarr3=[1,2,2,{a:1},{a:1},{a:1,b:2},function () {alert ("B");},function () {alert ("B");}];
Console.log (Distinctarrayall (TESTARR3));
result [1,2,{a:1},{a:1,b:2},function () {alert ("B");}]

Alas, finally successfully completed the task of the heavy, as for each method of performance problems, we leave to the next discussion! We can see that the last method is the Omnipotent method, can be heavy for complex arrays, but the corresponding execution cost is quite large, in the actual project development, sometimes we need may only be pure digits or pure string weight, which requires us to flexibly choose the appropriate algorithm, Do not seek too perfect, only to meet the needs of the foundation to make the program more efficient!

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.