JS Array to heavy __JS

Source: Internet
Author: User
Method 1:
function unique1 (array) {
        var newArr = [];
        Traversal array for
        (let key of array) {
            //If key has been saved to a temporary array, skip it, otherwise save it to a temporary array if
            (Newarr.indexof (key) = = 1) {
                Newarr.push (key)
            }
        return
        NEWARR
    }
Method 2:
function Unique2 (array) {
        //n is a hash table, NEWARR is a temporary array
        var n = {},newarr = [];
        For (let key of array) {if
            (!n[key]) {   //If there is no current item in the hash table
                N[key] = true;  Depositing hash table
                Newarr.push (key);  Push the current array item to a temporary array
            } return
        newArr;

    }

Note: cannot distinguish between 1 and ' 1 '

Unique2 ([1,2,4, ' 1 ', 2,4,3,2]   //  [1, 2, 4, 3]
Method 3: Using the Filter method
 function Unique3 (array) {var newArr = Array.filter (function (element, index, self) {
        return Self.indexof (Element) = = = Index});

    return NEWARR; }

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.