JavaScript tutorial: JavaScript array to weight problems

Source: Internet
Author: User

Article Introduction: JS Array to weight problem summary.

In the process of project development, 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.

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:

function Distinctarray (arr) {

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 data type in the process of going heavy.

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

var obj = {}, temp = [];
for (var i = 0; i < arr.length; i++) {
if (!obj[typeof (arr) + arr]) {
Temp.push (arr);
Obj[typeof (arr) + arr] = 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. The above coding process of our main idea is to use the JS object key is not repetitive concept 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:

function DistinctArray2 (arr) {

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:

function Distinctarrayall (arr) {

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, 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.