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
  • 相关阅读:
    Splunk数据处理
    使用http://start.spring.io/ 生成工程
    SpringBoot和SpringCloud区别
    Spring Cloud
    Spring Boot
    Splunk大数据分析经验分享
    Splunk和ELK深度对比
    Git 教程
    Docker 教程
    STL中的unique和unique_copy函数
  • 原文地址:https://www.cnblogs.com/helzeo/p/11820084.html
Copyright © 2011-2022 走看看