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