Home  >  Article  >  Web Front-end  >  What are the es6 set methods?

What are the es6 set methods?

青灯夜游
青灯夜游Original
2021-09-03 15:55:065596browse

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();

Example

let i = new Set();

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,}

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

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) {}

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

[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!

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