ES6 Array de-weight

Source: Internet
Author: User
Tags new set

Today 51, before going out to play hurriedly write a blog, always do not forget to learn ^_^!!

Refers to the array to weight, presumably everyone is not unfamiliar, will be the students may be crackling write several, the following to see before the common weight code:

' Use strict ';vararr = [1, ' A ', Undefined,NULL, nan,1, ' a ', Undefined,NULL, NaN]; Array.prototype.remDub= Array.prototype.remDub | |function(){     This. sort (); vararr = [ This[0]]; varBOk =true;  for(vari = 1; i< This. length;i++){        if( This[i]!== arr[arr.length-1]) {Arr.push ( This[i]); }    }    returnarr;};        Console.log (Arr.remdub ()); //[1, Nan, Nan, ' a ', null, undefined]

Above this is the most common on-line to go heavy, looks OK, is not the Nan to remove, do can use ... Some students will certainly say that how to do, we do the procedure is a rigorous matter, must pursue perfection,

So what can be solved? In fact, there are ... We can use this data structure to implement JSON, see the code below:

' Use strict ';vararr = [1, ' A ', Undefined,NULL, nan,1, ' a ', Undefined,NULL, NaN]; Array.prototype.remDub= Array.prototype.remDub | |function(){    varJSON = {};  for(vari = 0; i< This. length; i++) {json[ This[i]] | | (json[ This[i]] = This[i]); }    vararr = [];  for(varNameinchJSON)    {Arr.push (json[name]); }    returnarr;};        Console.log (Arr.remdub ()); //[1, ' A ', undefined, null, NaN]

Above this use of JSON can be achieved, in fact, Es6 did not come out before using this is good, but ES6 is the future development trend, so look at es6 how to achieve it:

 ' use strict ' ;  let  arr = [1, ' A ', Undefined,null , nan,1, ' A ', Undefined,null  ,nan "; Array.prototype.remDub  = Array.prototype.remDub | | function    () { return    Array.from (New Set (this));  return  [... new  Set (this   // [1, ' A ', undefined, null, NaN] 

Yes, with ES6 a few lines of code to solve the deduplication, using the set of this non-repeating data structure. The set internally determines whether two values are different, the algorithm used is called "Same-value equality", which is similar to the exact equality operator ( === ), and the main difference is that nan equals itself, while the exact equality operator considers Nan not equal to itself.

Here a lot of students will say, now a lot of browsers do not support ES6 syntax, use is also equal to white, then here I am in the introduction of something. It is called Babel;

First we install it through NPM.

So we can use the Babel command, of course, you can also change the file root directory to inject it into the dependency

Then install a babel-preset-es2015 module.

Then configure a. babelrc file in the root directory, configure the method inside

Yes, this is the second module we installed just now, we told Babel to use es2015 to convert;

Everything is ready, only one line of command, next we enter in the command line:

Removedub.js is just the ES6 syntax of the JS file, Babel conversion output ES5 syntax of the JS file.

Before conversion:

after conversion :

About more ES6 grammar, in fact, we can go to see Mr. Ruan wrote ECMAScript 6 into this.

ES6 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.