1. According to the principle of Key's non -repeated Key in the JS object, the method of remembered the array is as follows according to the most conventional thinking:
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]
It looks pretty good, but if it turns into a situation:
var testarr1 = [1,2,3, "2"];
console.log (distinctArray (testarr)); // [1,2,3]
It turned out to be the same result. This is not what we want. The result we need should be [1,2,3, "2"]. In the process of going heavy, you need to ensure the integrity of the type.
In response to the above, we improve 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 Typeof's prefix when the key is put in the object, so let's see the effect.
var testarr1 = [1,2,3, "2"];
console.log (distinctArray (testarr)); // [1,2,3, "2"]
Whoops, good! So is this function completely OK, let's see another situation!
var testarr1 = [1,2,3, "2", {a: 1}, {b: 1}];
console.log (distinctArray (testarr)); // [1,2,3, "2", {a: 1}]
This result, how to delete {b: 1} to inexplicably delete it. It is a very serious problem if you delete useful data by mistake during the heavy process, so the above method is not a kind of perfect, then then Let's look down.
2. In 1 in 1, our main idea is to use the concept of KEY in the JS object to guide our thinking, but in the end there is no solution to all problems. Then we can consider changing a thinking mode to realize the functions we want. Essence
Use the Slice and Splice methods to achieve the weight of the array, as follows:
Copy code code as follows:
Function Distancerray2 (ARR) {{
var test = Arr.slice (0); // The array copys a 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 this 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 (" b ");}]
The test results still cannot meet our needs. What should I do? After studying the above methods of our team, we found that the main problems are in the operation of comparing the two objects. Use in distributionArray2 "==" "" "to compare, and it cannot distinguish whether the content of the large object is equal. In view of this situation, we can, we, we, we, we, we, we can. Another method:
Copy code code as follows:
Function DistanceRrayall (ARR) {
var ISEQUAL = Function (obj1, obj2) {{
// Two objects are equal, and they must equal.
if (obj1 === obj2) {{
Return true;
}
if (typeof (obj1) == Typeof (obj2)) {{
if (ibj1) == "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 test = Arr.slice (0); // The array copys a copy to TEMP
for (var I = 0; I <temp.Length; i ++) {
for (j = i+1; j <temp.Length; j ++) {
ifqual (temp [j], temp [i]) {{) {{
temp.splice (j, 1); // Delete this 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 successfully completed the task of heavy weight. As for the performance problems of each method, we will stay for the next discussion! We can see that the last method is to emphasize the law, which can be important for complex arrays, but the corresponding execution overhead is also quite large. Sometimes what we need in actual project development may only be pure numbers or pure characters The string is heavy, which requires us to flexibly choose the corresponding algorithm according to the needs. Do not ask for too much perfect, only to make the program more efficient on the basis of meeting the needs!