A brief summary of the problem of JS array going heavy

Source: Internet
Author: User

  In the project development process often encountered in the array contains a lot of duplication of content, that is, dirty data to dirty operations, this article focuses on the array to the weight of several methods. Need friends can come to the reference, I hope to help you.

1. According to the principle of not duplicating the key in JS, the idea of several sets of methods of weight, according to the most conventional thinking is as follows:   code as follows: function Distinctarray (arr) {var obj={},temp=[]; for (Var i=0;i& lt;arr.length;i++) {if (!obj[arr[i]]) {Temp.push (arr[i]); Obj[arr[i]] =true;} return temp;   &NBSP}    var testarr=[1,2,3,2];    console.log (Distinctarray (Testarr))/[1,2,3]   looks good, but if it becomes a situation: Var testarr1=[1,2,3, "2"]; Console.log (Distinctarray (Testarr));/[1,2,3] is 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.   for the above situation, we improve the above methods:   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 a typeof prefix to the object, so let's look at the effect. var testarr1=[1,2,3, "2"]; Console.log (Distinctarray (Testarr));/[1,2,3, "2"] Oh, yes! 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 to give {b:1} to inexplicablyDeleted, the heavy process if the deletion of useful data is a serious problem, so the above method is not a kind of perfect, then let us go on to look down.   2. In 1, our main idea is to use JS object in the concept of not repeating key 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.   Use slice and splice methods to achieve the weight of the array, as follows: The   code is as follows: function DistinctArray2 (arr) {var temp=arr.slice (0);//array Copy 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");}]   test results still do not meet our needs, swollen? 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:   code is as follows: function Distinctarrayall (arr) {var isequal=function (obj1,obj2) {//Two object addresses are equal, must equal if (OBJ1===OBJ2) {return true;} if ( typeof (Obj1) ==typeof (obj2)) {if (typeof) = = "Object" obj1 (&&typeof) = "Object ") {var pcount=0; for (Var p. 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; The Var temp=arr.slice (0);//array copies a copy 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)); Results [1,2,{a:1},{a:1,b:2},function () {alert ("B");}]   Oops, finally finished the heavy task, 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.