zoukankan      html  css  js  c++  java
  • es6-11 map set 与数组和对象的比较

    map 与 Array 增查改删的对比

    {
        let map = new Map()
        let array = []
    
        //
        map.set('t', 1)
        array.push({t: 1})
        console.log('map', map) // Map(1) {"t" => 1}
        console.log('array', array) // [t: 1]
    
        //
        let map_exist = map.has('t'); // 检测 map 是否有 t
        let array_exist = array.find(item => item.t) // 检测数组是否有 t
        console.log(map_exist) // true
        console.log(array_exist) // {t: 1} 注意这里是返回查询到的数据, 而不是布尔值
    
        //
        map.set('t', 2) // map 直接用 set 更改
        array.forEach(item => item.t ? item.t = 2 : '')
        console.log('map', map) // Map(1) {"t" => 2}
        console.log('array', array) // [t: 2]
    
        //
        map.delete('t')
        let index = array.findIndex(item => item.t) // 先找到删除单元的索引
        array.splice(index, 1) // 在用 splice 删除指定元素
        console.log('map', map) // Map(0) {}
        console.log('array', array) // []
    }

    set 与 Array 增查改删的对比

    {
        let set = new Set()
        let array = []
    
        //
        set.add({t: 1}) // 注意这里是对象, 不是 key value
        array.push({t: 1})
        console.log(set)
        console.log(array)
    
        //
        let set_exist = set.has({t: 1}) // {t: 1} 没有被保存过所以这里查的是 false
        let array_exist = array.find(item => item.t)
        console.log(set_exist)
        console.log(array_exist)
    
        //
        set.forEach(item => item.t ? item.t = 2 : '')
        array.forEach(item => item.t ? item.t = 2 : '')
        console.log(set)
        console.log(array)
    
        //
        set.forEach(item => item.t ? set.delete(item) : '')
        let index = array.findIndex(item => item.t)
        array.splice(index, 1)
        console.log(set)
        console.log(array)
    }

    数据结构横向对比 增 查 改 删 set map 和 Object 的对比

    {
        let item = {t: 1}
        let map = new Map()
        let set = new Set()
        let obj = {}
    
        //
        map.set('t', 1) // map 注意键值对形式
        set.add(item) // set 注意是对象
        obj.t = 1
        console.log(map)
        console.log(set)
        console.log(obj)
    
        //
        console.info({
            map_exist: map.has('t'), // true
            set_exist: item, // true 这里 item 在上边被保存过
            obj_exist: 't' in obj // {t: 1}
        })
    
        //
        map.set('t', 2)
        item.t = 2 // 注意 set 是引用它的, 所以改它就改变了 set
        obj.t = 2
        console.log(map)
        console.log(set)
        console.log(obj)
    
        //
        map.delete('t')
        set.delete(item)
        delete obj.t
        console.log(map)
        console.log(set)
        console.log(obj)
    }
    总结建议: 在数据开发过程中设计的数据结构, 能使用 map, 不使用 Array 和 Object, 其次如果对数据结构要求存储的唯一性则考虑使用 set
  • 相关阅读:
    罗美琪和春波特的故事...
    欢迎参与 KubeVela 官方文档翻译活动
    开源 1 年半 star 破 1.2 万的 Dapr 是如何在阿里落地的?
    Service Mesh 从“趋势”走向“无聊”
    Nacos 2.0 性能提升十倍,贡献者 80% 以上来自阿里之外
    阿里巴巴云原生 etcd 服务集群管控优化实践
    KubeVela 1.0 :开启可编程式应用平台的未来
    知行动手实验室可以用来做什么?
    7. Jackson用树模型处理JSON是必备技能,不信你看
    Linux:sudo,没有找到有效的 sudoers 资源
  • 原文地址:https://www.cnblogs.com/helzeo/p/11820084.html
Copyright © 2011-2022 走看看