Two methods for JavaScript to judge the contents of an array (recommended) _javascript tips

Source: Internet
Author: User

Objective

In general, we may give the array weight, this operation is not complex, the implementation of a loop is. Now, what I'm going to do is to determine if there are duplicates in the array, and if so, return True or false.

Ideas

To turn an array into a string

Loop the original array, take each field and this string to compare to see if there are duplicates

How do I compare a string with a B string and ask to determine if the B string contains a string of a.

Method a indexOf () and LastIndexOf () contrast method.

First, we build the code:

var arr = ["AA", "BB", "cc", "BB", "AA"];
Arrrepeat (arr);

As above, we're going to use a arrrepeat (ARR) validation function and execute it, and build this function here

function Arrrepeat (arr) {
var arrstr = json.stringify (arr), str;
for (var i = 0; i < arr.length i++) {
if (Arrstr.indexof (arr[i)!= arrstr.lastindexof (arr[i))) {return
true ;
}
};
return false;
}

OK, run successfully.

The principle is particularly simple, that is, the field in the array, in the string into the array of the first occurrence of the position and the last occurrence of the position is consistent, if not consistent, it shows that the repetition occurred.

Method two match () regular Contrast method

First, as with the above, we build the code:

var arr = ["AA", "BB", "cc", "BB", "AA"];
Arrrepeat (arr);

We then reconstruct the arrrepeat (arr) function

function Arrrepeat (arr) {
var arrstr = json.stringify (arr), str;
for (var i = 0; i < arr.length i++) {
if (Arrstr.match (new RegExp (Arr[i), "G"). Length) >1) {return
True;
   }
};
return false;
}

The principle is to find a certain number of repetitions, if it is greater than 1, it must be repeated. Note that here is able to accurately find out how many times the Oh! So, this method actually has a wider use.

OK, run another success

Summarize

If only the first method is actually enough.

The second method can find the actual number of occurrences, such as repeating 4 times, to find 4. The specific use of their own thinking slightly.

Building a regular method that contains a variable new REGEXP (Arr[i], "G") is also asked by others.

In fact, I thought first is the second idea, the problem of the regular problems for half a day, finally resolved. Just think of the first idea.

The above is a small set of JavaScript to tell you how to judge the array of repeated content of the two methods (recommended), I hope to help everyone!

Related Article

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.