es6数据结构map源码封装

本文深入探讨了ES6中Map数据结构的特点与应用,通过自制Map实现,详细讲解了其内部工作原理,包括键值对的存储、查找、删除等操作,并对比了原生Map与自制Map的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script>
    // let arr = [1, 2, 3];
    // //特点 有序,不定长,支持栈队列操作
    // //FILO arr.push arr.pop
    // //FIFO arr.push arr.shift

    // //ES6新的数据结构
    // //Map Set
    // //Map 字典,加强的对象
    // let map = new Map();
    // let obj = {};
    // map.set(1, 1);
    // obj["1"] = 1;
    // // 赋值
    // map.set(1, "aa");
    // obj["1"] = "aa";
    // //输出
    // console.log(map.get(1))
    // console.log(obj["1"])

    // console.log(map, obj);

    // //若干键值对的集合
    // console.log(map.has(1));
    // console.log(map.has("1"))
    // console.log(map.has(2))
    //     //删除
    // map.delete(1)
    // console.log(map.has(1))
    // map.clear()

    //hash 算法
    function myMap() {
        this.init();
    }
    myMap.prototype.len = 8;
    myMap.prototype.bucket = [];
    myMap.prototype.init = function() {
        for (let i = 0; i < this.len; i++) {
            this.bucket[i] = {
                next: null
            }
        }
    }
    myMap.prototype.makeHash = function(key) {
        let hash = 0
        if (typeof key == "string") {
            //取后三位字符串
            let len = (key.length > 3) ? key.length : 3;
            for (let i = len - 3; i < len; i++) {
                hash += (key[i] !== undefined ? key[i].charCodeAt() : 0)
            }
        } else {
            hash = +key
        }
        return hash;
    }
    myMap.prototype.set = function(key, value) {
        let hash = this.makeHash;
        let list = this.bucket[hash % this.len];
        let nextNode = list;
        while (nextNode.next) {
            if (nextNode.key === key) {
                nextNode.value = value;
                return
            } else {
                nextNode = nextNode.next;
            }
        }
        nextNode.next = {
            key,
            value,
            next: null
        }
    }
    myMap.prototype.get = function(key) {
        let hash = this.makeHash;
        let list = this.bucket[hash % this.len];
        let nextNode = list;
        while (nextNode.next) {
            if (nextNode.key === key) {
                return nextNode.value;
            } else {
                nextNode = nextNode.next;
            }
        }
        return
    }
    myMap.prototype.has = function(key) {
        let hash = this.makeHash;
        let list = this.bucket[hash % this.len];
        let nextNode = list;
        while (nextNode.next) {
            if (nextNode.key === key) {
                return true;
            } else {
                nextNode = nextNode.next;
            }
        }
        return false;
    }
    myMap.prototype.deleat = function(key) {
        let hash = this.makeHash;
        let list = this.bucket[hash % this.len];
        let nextNode = list;
        while (nextNode.next) {
            if (nextNode.next.key === key) {
                nextNode.next = nextNode.next.next;
                return true;
            } else {
                nextNode = nextNode.next;
            }
        }
        return false;
    }
    myMap.prototype.clear = function() {
        this.init();
    }
    let map = new myMap();
</script>复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值