Set implements array de-weight

Source: Internet
Author: User

  1. Set data structure is added to the >ES6, similar to an array, but   its members are unique &NBSP, whose constructor can accept an array as a parameter, such as

      let array = [1, 1, 1, 1, 2,  3, 4, 4, 5, 3]; let set = new Set (array); console.log (set); //= = Set {1, 2, 3, 4, 5}          
  2. 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] 

Ext.: http://www.cnblogs.com/raocheng/articles/6549556.html
/span>

Set to implement 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.