JS array excluding duplicate values (string)

Source: Internet
Author: User

Premise: The element type in the array is: string

Read on the Internet a lot of tall still articles, or can not solve my string arry problem, only blame their brains, on the code:

<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />    <title>JQ array string de-weight</title>    <Scriptsrc= "Jquery-1.7.2.min.js"></Script>    <Scripttype= "Text/javascript">        $(function () {            varSbarray= NewArray (["Beijing"], ["Shijiazhuang"], ["Tianjin"], ["Beijing"], ["Shijiazhuang"], ["Tianjin"], ["Baoding"], ["Beijing"], ["Handan"], ["Baoding"], ["Langfang"], ["Baoding"]); varSB= NewArray ();            alert (sbarray.length); Alert (Sbarray.indexof ("Beijing")); //iterating through an array            $('input'). Click (function () {                if(Sbarray.length> 0) {                     for (varI= 0; I<sbarray.length; I++) {                        if(Sb.tostring (). IndexOf (Sbarray[i])< 0) {Sb.push (sbarray[i]); }                    }//End for                    $("#p2"). Text (sb.tostring ()); }//End If            });    }); </Script></Head><Body>    <inputtype= "button"value= "Row Weight" />    <PID= "P2"></P></Body></HTML>

Note: In the Sbarray array, the return value of Sbarray.indexof ("Beijing") is-1, which is not included, and it does not appear to be directly judged by the indexOf ().

In order to achieve the effect, a ToString () method was added to convert the indexof () board to a string after it was forced to use.

This is my own thinking of the stupid method, of course, its time complexity may not meet the requirements of everyone, do not spray.

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.