ES6 through set array to remove weight

Source: Internet
Author: User
Tags iterable new set
First, Set1. Definition

The set object is a newly defined data structure in ES6, similar to an array, which allows you to store a unique value of any type, whether it is a raw value or an object reference.

2. Syntax
new Set([iterable])
  • Iterable: Object can be iterated, default is empty.
Set method
  • Add: Adds a value that returns the set itself.
  • Delete: Deletes the value and returns whether the deletion succeeded.
  • Has: Determines if it has this value and returns TRUE/FALSE.
  • Clear: Clears all values.
3. Example
let s = new Set();s.add(4);s.add(1);s.add(3);s.add(3);s.add(2);s.add(2);console.log(s); // {4, 1, 3, 2}console.log(s.has(4)); // trues.delete(4);console.log(s); // {1, 3, 2}console.log(s.has(4)); // falses.clear();console.log(s); // {}
Second, through the set array to weight

The extension operator allows you to convert a set into a true array.

let arr = [4, 1, 3, 3, 2, '2'];let uniqueArr = [...new Set(arr)];console.log(uniqueArr); // [4, 1, 3, 2, "2"]

ES6 through set array to remove 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.