什么是Map?什么是Set?

103 阅读2分钟

集合Set

  1. Set的创建add()
 // set的创建1
        const set = new Set()
        // 添加
        set.add(1)
        set.add(2)
        console.log(set);
                                            // Set(2) {1, 2}
// set的创建2
        const set1 = new Set([3,4])
        console.log(set1);
                                            // Set(2) {3, 4}
  1. Set元素查看has()
// 查看有无元素
        const set = new Set([1,2])
        set.add(3)
        console.log(set.has(1));
                                            //true
// 有的话返回true,没有的话返回false

3.查看长度size

 // 查看长度
        const set = new Set([1,2])
        set.add(3)
        console.log(set.size);
                                            // 3
  1. 删除某一元素delete()
 // 删除某一元素
        const set = new Set([1,2])
        set.add(3)
        set.delete(1)
        console.log(set);
                                            // Set(2) {2, 3}

5.转化成数组 [...set],Array.from(set)

// 转化成数组
        const set = new Set([1,2])
        set.add(3)
        console.log([...set]);
        console.log(Array.from(set));
                                            //(3) [1, 2, 3]

6.Set去重

// Set去重
        const arr = [1,2,3,4,5,6,7,1,2,3]
        console.log([...new Set(arr)]);
                                            // [1, 2, 3, 4, 5, 6, 7]
// 注意 引用类型不能去重,引用类型存入的是指针,指针不一致
        const arr1 = [1,2,3,4,{},{}];
        console.log([...new Set(arr1)]);
                                            // [1, 2, 3, 4, {…}, {…}]
// 注意 NaN != NaN ,但是Set也会去重
        const arr2= [1,2,3,4,NaN,NaN];
        console.log([...new Set(arr2)]);
                                            // [1, 2, 3, 4, NaN]

7.删除所有的元素

 const set = new Set([1,2,3,4,56,7])
        set.clear()
        console.log(set);
                                            // Set(0) {size: 0}

8.数组的遍历

// 获取数组键值对
        const arr = new Set([1,2,3,4,1,2])
        for(let elem of arr.entries()){
            console.log(elem);
        }
                                                // (2) [1, 1]
                                                // (2) [2, 2]
                                                // (2) [3, 3]
                                                // (2) [4, 4]
// 获取数组的值
        const arr = new Set([1,2,3,4,1,2])
        for(let elem of arr.values()){
            console.log(elem);
        }
                                                // 1
                                                // 2
                                                // 3
                                                // 4
const arr = new Set([1,2,3,4,1,2])
arr.entries()
                                                // 1
                                                // 2
                                                // 3
                                                // 4

字典Map

  1. Map的创建,添加set() 键值可以是任意数据类型、查找 get()、长度获取size、删除某一元素delete()、全部删除clear()
// Map的创建,添加set() 键值可以是任意数据类型,查找 get()
        const map = new Map()
        map.set(1,'张三')
        map.set(2,18)
        map.set(3,["性别",'男']) //键名一致会替换
        map.set(1,'李四')
        map.set(4,{})
        console.log(map);
        
// 查找 get()
        console.log(map.get(3));
                                              // ['性别', '男']
//   长度获取
            console.log(map.size);
//   删除某一元素
            map.delete(4)
            console.log(map);
//  全部删除
            map.clear()
            console.log(map);

2.Map的遍历

 // 遍历数组
        for(let i of map.entries()){
            console.log(i);
        }
// 获取键值values()
        for(let i of map.values()){
            console.log(i);
        }
        map.forEach(k=>{
            console.log(k);
        })
// 获取键名keys()
        for(let i of map.keys()){
            console.log(i);
        }

如何应用?

1.封装一个函数,有一个数组,去除相同的字符串返回结果?

  • Set方法实现
const arr = [1,2,3,4,5,6,7,8,4,3,8,8,8,9]
function Eliminate(list){
            const setList = new Set()
            for(let i=0;i< list.length;i++){
            setList.add(arr[i])
        }
            const newList =[...setList]
            return newList
        }
        console.log(Eliminate(arr));
  • Map方法实现
const arr = [1,2,3,4,5,6,7,8,4,3,8,8,8,9]
function Eliminate(list){
            const mapList = new Map()
            const newList = []
            for(let i=0;i<list.length;i++){
                mapList.set(list[i],list[i])
            }
            mapList.forEach(elem=>{
                newList.push(elem)
            })
            return newList
        }
        console.log(Eliminate(arr));

2.封装一个函数,有一个数组,去除相同的字符串返回去掉的字符串?

  • Set方法实现
const arr = [1,2,3,4,5,6,7,8,4,3,8,8,8,9]
function Repetition(list){
            const setList = new Set()
            const newList = []
            for(i=0;i<list.length;i++){
                if(setList.has(list[i])){
                    newList.push(list[i])
                }else{
                    setList.add(list[i])
                }
            }
            return newList
        }
        console.log(Repetition(arr));
  • Map方法实现
const arr = [1,2,3,4,5,6,7,8,4,3,8,8,8,9]
function Repetition(list){
            const mapList = new Map()
            const newList = []
            for(i=0;i<arr.length;i++){
                if(mapList.has(list[i])){
                    newList.push(list[i])
                }else{
                    mapList.set(list[i],list[i])
                }
            }
            return newList
        }
        console.log(Repetition(arr));