Home > Web Front-end > JS Tutorial > body text

What are the es6 set methods?

青灯夜游
Release: 2023-01-07 11:47:08
Original
5603 people have browsed it

es6 set methods can be divided into two categories: 1. Operation methods "add(value)", "delete(value)", "has(value)", clear(); 2. Traversal method keys (), values(), entries(), forEach().

What are the es6 set methods?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

ES6 provides a new data structure Set. It is similar to an array, but the values ​​of the members are unique and there are no duplicate values.

Many times we call Set a set. However, a Set can be a set, but a set is not necessarily a Set.

Features: Uniqueness=>No duplication=>Ability to deduplicate data.

Creating a Set

Set itself is a constructor, and the constructor is called to generate the Set data structure.

关键词 标识符 = new Set();
Copy after login

Example

let i = new Set();
Copy after login

Set function can accept an array (or array-like object) as a parameter for data initialization.

let i = new Set([1, 2, 3, 4, 4]);  //会得到  set{1, 2, 3, 4,}
Copy after login

Note: If there are duplicate values ​​given during initialization, they will be automatically removed. Collections do not have a literal declaration method and can only be declared using the new keyword.

Attributes of Set

There is only one commonly used attribute: size--returns the total number of members of the Set instance.

let s = new Set([1, 2, 3]);
console.log( s.size ); // 3
Copy after login

Set methods

The methods of Set instances are divided into two categories: operation methods (for data operations) and traversal methods (for traversal data).

Operation method:

  • add(value)  Add data and return a new Set structure

  • delete(value) Delete data and return a Boolean value, indicating whether the deletion was successful

  • has(value) Check whether a certain data exists and return a Boolean value

  • clear()   Clear all data, no return value

let set = new Set([1, 2, 3, 4, 4]);
// 添加数据 5
let addSet = set.add(5);
console.log(addSet); // Set(5) {1, 2, 3, 4, 5}

// 删除数据 4s
let delSet = set.delete(4);
console.log(delSet); // true 此处返回值是个boolean 表示 是否删除成功

// 查看是否存在数据 4
let hasSet = set.has(4);
console.log(hasSet); // false

// 清除所有数据
set.clear();
console.log(set); // Set(0) {}
Copy after login

Traversal method:

Set provides three The traverser generates functions and a traversal method.

  • keys()   Returns a traverser of key names

  • values()   Returns a traverser of key values

  • entries()  Return a traverser of key-value pairs

  • forEach()  Use the callback function to traverse each member

let color = new Set(["red", "green", "blue"]);
for(let item of color.keys()){
 console.log(item);
}
// red
// green
// blue
for(let item of color.values()){
 console.log(item);
}
// red
// green
// blue
for(let item of color.entries()){
 console.log(item);
}
// ["red", "red"]
// ["green", "green"]
// ["blue", "blue"]
color.forEach((item) => {
 console.log(item)
})
// red
// green
// blue
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What are the es6 set methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!