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

Introduction to the usage of Set in ES6 (code example)

不言
Release: 2018-11-14 16:13:02
forward
2343 people have browsed it

This article brings you an introduction to the usage of Set in ES6 (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Overview

Set is a new data structure, which has similar characteristics to other languages. Of course, as a Set in js, it still has some characteristics of js.

Initialization

new Set([iterable]);
Copy after login

Initializing a Set has an optional parameter. This parameter must be an iterable object. Iterable objects include String, Array, Array-Like obejct(Arguments, NodeList ), Typed Array, Set, Map and user-defined iterable objects

String

new Set('1234') // Set(4) {"1", "2", "3", "4"}
Copy after login

Array

new Set([1,2,3,4]) // Set(4) {1, 2, 3, 4}
Copy after login

arguments

function sum(){
  return new Set(arguments)
}
sum(1,2,3,4)  // Set(4) {1, 2, 3, 4}
Copy after login

Set

new Set(new Set([1,2,3,4])) // Set(4) {1, 2, 3, 4}
Copy after login

Add value

After initializing a Set, you can continue to add values ​​to it

let set=new Set()
set.add(1)
set.add(1)
set.add(1)
console.log(set) // Set(1) {1}
Copy after login

You can do many things by borrowing this feature, such as filtering duplicate values

new Set([1,1,2,3]) // Set(3){1,2,3}
Copy after login

But Note that two identical object literals are different objects with different references, so Set cannot mark two objects with different references as the same, even if they appear to be the same

let a={num:1}
let b={num:1}
console.log(a===b) //false
new Set(a, b)// Set(2){{num:1},{num:2}}
let c=a
console.log(c===a)//true
new Set(a,c)// Set(1){{num:1}}
Copy after login

Judge whether it contains

let set=new Set([1,2,3])
set.has(1) // true
set.has(4) //false
Copy after login

Get the quantity

let set=new Set([1,2,3])
set.size //3
Copy after login

Delete

let set=new Set([1,2,3])
set.delete(1)// true
set.delete(1)// false
Copy after login

Clear

let set=new Set([1,2,3])
set.clear()
console.log(set) // Set(0){}
Copy after login

Traverse

let set=new Set([1,2,3])
set.forEach((s)=>{console.log(s)})
// 1
// 2
// 3
Copy after login

Get Iterator

let set=new Set([1,2,3])
let entries=set.entries()
console.log(entries.next().value) // [1,1]
console.log(entries.next().value) // [2,2]
console.log(entries.next().value) // [3,3]
console.log(entries.next().value) // undefined

for(let item of set){
    console.log(item)
}
// 1
// 2
// 3
Copy after login

Get key iterator

let set=new Set([1,2,3])
let keys=set.keys()
console.log(keys.next().value) // 1
console.log(keys.next().value) // 2
console.log(keys.next().value) // 3
console.log(keys.next().value) // undefined

for(let {key} of set){
    console.log(key)
}
// 1
// 2
// 3
Copy after login

Get value iterator

let set=new Set([1,2,3])
let values=set.values()
console.log(values.next().value) // 1
console.log(values.next().value) // 2
console.log(values.next().value) // 3
console.log(values.next().value) // undefined

for(let {value} of set){
    console.log(value)
}
// 1
// 2
// 3
Copy after login

The above is the detailed content of Introduction to the usage of Set in ES6 (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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