Home >Web Front-end >JS Tutorial >Introduction to the usage of Set in ES6 (code example)

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

不言
不言forward
2018-11-14 16:13:022350browse

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

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

Array

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

arguments

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

Set

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

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}

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}

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

Judge whether it contains

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

Get the quantity

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

Delete

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

Clear

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

Traverse

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

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

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

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

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete