Summary of the method of the JS array de-weight

Source: Internet
Author: User
Tags prev

Want to precipitate yourself. Code changes the World ~ ~ ~

1, the use of Array.from code as follows

1         var arr = [1,2,3,4,3,2,1]2     function  Unique (arr) {3         return Array.from (new  Set (arr))4    }5     Console.log ( Unique (arr))

2, the use of ES6 (... ) to expand the operator. Demo

1 var arr = [1,2,3,4,3,2,1]2     function  Unique (arr) {3         return [... New Set (arr)] 4     }5     console.log (Unique (arr))

3, the use of Object.keys (); Directly on the code

var obj = {},newarr,arr = [1,2,3,4,5,3,2,1];      for (var i = arr.length-1; I >= 0; i--) {        null;    };     = Object.keys (obj);    Console.log (NEWARR)// The problem here is that it will be converted into a string. Look at the situation and use it yourself. Of course it can be handled.

4, more than the method of good. Similar to the Third Kind ().

var arr = [1,2,3,4,3,2,1]    var o  = {},arr1 = [];      for (var i = arr.length-1; I >= 0; i--) {        if(!  O[arr[i]]) {            true;            Arr1.push (Arr[i])        }    };    Console.log (ARR1)

5, this is I suddenly found. Come (here is the moment to witness the miracle) (this is the time to read the MDN after the introduction of reduce, I think, the results have been achieved.) I feel very successful. La La la la)

var arr = [1,2,3,4,3,2,1]var newarr =    arr.reduce (function(prev,now) {         if (Prev.indexof (now) <0) {Prev.push (now)} return prev    },[])    Console.log (newarr)

In fact, there are many ways to remove the weight of JS array. Those loops are blah, not here to show. These kinds of methods are relatively new. At least that's the way he thinks hooves. It's like starting to say that. I want to settle myself. Because sometimes I feel a little confused. Try to relax yourself by writing a blog code. Find yourself. Hope that they can continue to refuel, and then try to share some of their own experience and harvest every week. I want to be a qualified front-end person here. Thank you.

This article can be reproduced in full text, but need to retain both the original author and source.

Summary of the method of the JS array de-weight

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.