One line of code implements array de-weight (ES6)

Source: Internet
Author: User

Tag:for   node   each    Code    strong     A line of code    ber   color   class   

  1. The Set data structure is new in
  2. ES6, similar to an array, but   its members are unique   its constructors can accept an array as an argument, such as:

      let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, Span class= "Hljs-number" >3]; let set = new Set (array); console.log (set); //= = Set {1, 2, 3, 4, 5}     
  3. Arraya static method has been added to the ES6 to Array.from convert an array-like object into arrays, such as through querySelectAll methods HTML DOM Node List , as well as new Set and Map other traversed objects in the ES6, such as:

    let set = new Set(); set.add(1).add(2).add(3); let array = Array.from(set); console.log(array); // => [1, 2, 3]

So now we can use a line of code to achieve the weight of the array:

let array = Array.from(new Set([1, 1, 1, 2, 3, 2, 4]));console.log(array);// => [1, 2, 3, 4]

Attached: ES5 implementing array de-weight

var array = [1, ‘1‘, 1, 2, 3, 2, 4];var tmpObj = {};var result = [];array.forEach(function(a) { var key = (typeof a) + a; if (!tmpObj[key]) { tmpObj[key] = true; result.push(a); }});console.log(result);// => [1, "1", 2, 3, 4]

One line of code implements array de-weight (ES6)

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