Using set to achieve de-weight

Source: Internet
Author: User

var arr=[1,2,1,3,2,4,5,1,3]; var setarr=New  Set (arr); var newarr=[];setarr.foreach (function(e) {    newarr.push (e);}); Console.log (NEWARR);

Set is the concept of a new set proposed in ES6, similar to the array arr, which is also not just a numeric type, other types of data such as strings are also supported, set has an array of different attributes is that duplicate elements are not valid in the collection, so an Using Set.add to add to this set automatically implements the step of de-weight.

The following are Set all supported operations:

    • new Set: Creates a new, empty Set .
    • new Set(iterable): Extracts elements from any data that can be traversed to construct a new collection.
    • set.size: Gets the size of the collection, which is the number of elements in it.
    • set.has(value): Returns a Boolean value that determines whether the specified element is contained in the collection.
    • set.add(value): Add an element. If it is duplicated, it does not produce an effect.
    • set.delete(value): Deletes an element. If it does not exist, it does not produce an effect. .add()and .delete() will return the collection itself, so we can use the chain syntax.
    • set[Symbol.iterator](): Returns a new iterator that iterates through the entire collection. In general, this method is not called directly, because it actually enables the collection to be traversed, that is to say, we can write directly, and for (v of set) {...} so on.
    • set.forEach(f): Just explain it in code, it's like a for (let value of set) { f(value, value, set); } shorthand, an array-like .forEach() method.
    • set.clear(): Empties the collection.
    • set.keys(), set.values() and set.entries() returns various iterators, which are provided for compatibility Map

This article transferred from: http://www.cnblogs.com/weblv/p/5192178.html

Using set to achieve 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.