Set和Map数据结构

1//Symbol数据类型 描述

let symbol1 = Symbol(1);
    console.log(symbol1);

//Symbol应用 私有属性

function Perent() {
        let a = Symbol();
        this[a] = '王';
        this.getA = () => {
            return this[a];
        }
    }
    let p = new Perent();
    console.log(p.fangfa());

2// 集合结构 Set 引用数据类型 内数据具有唯一性

  let set1 = new Set();
    console.log(set1);

(1) //Set 方法

set1.add(10).add(20).add(30).delete(10);
    console.log(set1, set1.size);
    let set2 = set1;
    set2.clear();
    console.log(set1, set2);

(2)//forEach

let set3 = new Set();
    set3.add(10).add(20).add(40);
    set3.forEach((v, k) => {
        console.log(v, k);//v==k 留一个就行
    })

3 //数组去重
//01 法

let shuZhu = [10, 10, 10, 40, 90, 90, 90, 100];
let shuZhu1 = [];
    for (let i = 0; i < shuZhu.length; i++) {
        if (shuZhu1.indexOf(shuZhu[i]) == -1) {
            shuZhu1[shuZhu1.length] = shuZhu[i];
        }
    }
 console.log(shuZhu1);

//02 法

let shuZhu = [10, 10, 10, 40, 90, 90, 90, 100];
    for (let i = 0; i < shuZhu.length; i++) {
        for (let j = i + 1; j < shuZhu.length; j++) {
            if (shuZhu[i] == shuZhu[j]) {
                shuZhu.splice(j, 1);
                j--;
            }
        }
    }
    console.log(shuZhu);

//03 法 Set内数据具有唯一性

let shuZhu = [10, 10, 10, 40, 90, 90, 90, 100];
    let set = new Set(shuZhu);
    console.log(Array.from(set));

//04 法

    let diandian = [...new Set(shuZhu)];
    console.log(diandian);

你可能感兴趣的:(Set和Map数据结构)