Set数据结构,注意这里不是数据类型,而是数据结构。Set的数据结构是以数组的形式构建的

Set的声明

Set和Array 的区别是Set不允许内部有重复的值,如果有只显示一个,相当于去重。虽然Set很像数组,但是他不是数组。

        let setArr = new Set(['jie','biao','nine','jie']);
        console.log(setArr) //Set(3) {"jie", "biao", "nine"}

Set值的增删查

add增加

        let setArr = new Set(['jie', 'biao', 'nine', 'jie']);
        setArr.add("杰")
        console.log(setArr) //Set(4) {"jie", "biao", "nine", "杰"}

delete删除

        let setArr = new Set(['jie', 'biao', 'nine', 'jie']);
        setArr.add("杰")
        setArr.delete("杰")
        console.log(setArr) //Set(3) {"jie", "biao", "nine"}

has查找

        let setArr = new Set(['jie', 'biao', 'nine', 'jie']);
        console.log(setArr.has('jiee')) //false
        console.log(setArr.has('jie')) //true

clear全部清除

        let setArr = new Set(['jie', 'biao', 'nine', 'jie']);
        setArr.clear()
        console.log(setArr) //Set(0) {}

size

        let setArr = new Set(['jie', 'biao', 'nine', 'jie']);
        console.log(setArr.size) //3

forEach

        let setArr = new Set(['jie', 'biao', 'nine', 'jie']);
        setArr.forEach((item => {
            console.log(item)
        }))
        // jie biao nine

渣渣辉
1.3k 声望147 粉丝