Three easy-to-use array-to-weight methods

Source: Internet
Author: User
Tags object object

var a = [5,6,5,4, ' 3 ', {},3, ' character ']   
Method One: Create a new array, use IndexOf to determine whether it exists in the new array, not present the push to the new array, and finally return the new array
    function Unique0 () {        = [];          for (Let i = 0;len = A.length,i < Len;i + +)            {if(Arr.indexof (a[i]) = = = 1) {                arr.push (A[i])            }        }         return arr    }    Console.log (Unique0 (a))
Method Two: Use Object object to save the array value, determine whether the array value has been saved in the object, not saved then push to the new array and record the Save in Object[arrayitem]=1 way
    function Unique1 (a) {        = [];         = {};          for (Let i = 0;len = A.length,i < Len; i + +)            {typeof A[i] + a[i            ]if(!
   
    hash[key]) {                = 1
    ;                Ret.push (A[i])            }        }        
    return 
     ret    }    Console.log (Unique1 (a) )
   

Method Three: ES6 new data structure set, new method Array.from
    function Unique2 (a) {        new  Set (a)        return  array.from (arr)    }    Console.log (Unique2 (a))

Three easy-to-use array-to-weight methods

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.