Javascript data structures and algorithms Learning (Four) - a collection of

What is a collection?

Before the data structure review: stacks, queues, linked lists - they can use JS array to achieve, and achieve more dependent on the set of Object, compared to the target key: value of the structure, key and value of the collection is in fact the same that {1:1,2:2,'a':'a'}this kind. Reference Set data structure for ES6

  • characteristic:
    1. No repetitive: each element is mutually exclusive
    2. Disorder: a collection, each position by valuable are the same, is disordered between elements.
    3. JS represented in the collection: objects

The method of operation set

  • Whether there is: has (value)
  • Delete: remove (value)
  • Add: add (value)
  • Index: indexOf ()
  • Clear: clear ()
  • Length Size: size
function SetClass(){
    var items= {}
    this.has = function(val){
        return items.hasOwnProperty(val)
    }
    this.add = function(val){
        if(!this.has(val)){
            items[val]=val; // 以值 为key
            return val
        }
        return false
    }
    this.remove = function (value){
        if(!this.has(value)){
            delete items[value]
            return true;
        }
        return false;
    }
    this.clear=function(){
        items = {}
    }
    this.size = function(){
        return Object.keys(items).length;
    }
    this.values = function(){
        var values = []
        for(let key in items){
            if(items.hasOwnProperty(items[i])){
                values.push(items[i])
            }
        }
        return values
    }
    this.print = function(){
        console.log(items)
    }
} 

Operation between collections

Here Insert Picture Description

  • Union: union
  • Intersection: intersection
  • 差集:difference

Union

Here Insert Picture Description

 this.union = function (otherSet){
       var resSet = new SetClass()
       var arr = this.values()
       for(let i=0;i<arr.length;i++){
           resSet.add(arr[i])
       }
       arr = otherSet.values()
       for(let i=0;i<arr.length;i++){
           resSet.add(arr[i])
       }
       return resSet
   }

Intersection

this.intersection = function(otherSet){
        var resSet = new SetClass()
        var arr = this.values()
        for(let i=0;i<arr.length;i++){
            if(!otherSet.has(arr[i])){
                resSet.add(arr[i])
            }
        }
        return resSet
    }

Difference set

 this.differenct = function(otherSet){
        var resSet = new SetClass()
        let values = this.values()
        for(let i=0;i<values.length;i++){
            if(!otherSet.has(values[i])){
                resSet.add(values[i])
            }
        }
        return resSet
    }

ES6 built a new data structure

  • Set: collection
  • WeakSet: weak collection, can add value as an element of an object type

Related API operations
Here Insert Picture Description

var set = new Set(([1,2,3]))
set.forEach((key,value,set)=>{
    console.log('key---',key)
    console.log('value---',value)
    console.log(set)
})

var weakSet = new WeakSet()
weakSet.add('1') //报错
weakSet.add({name:'a'})

ES6 realize the intersection, union, difference

var set1 = new Set(([1,2,3]))
var set2 =new Set([2,3,4])

// 交集
var intersect = new Set([...set1].filter(item=>set2.has(item)))
// 差集
var different = new Set([...set2].filter(item=>!set1.has(item)))
// 并集
var union = new Set([...set1,...set2])

Set the difference and WeakSet

  • Set a strong reference and WeakSet weak references
    Here Insert Picture Description

Above, declare a variable pointing to an object obj {name:''ceshi}, obj is added to the set configuration, to change the point of obj null, at this time, because the structure of the original Set strong reference point to the original object, so the object is not recovered the GC, to dereference after remain in memory until use set.delete delete the object. The weakSet is changed after pointing obj, the original object ignores weak references weakset is directly recycled GC

  • Scenario: development, use weakSet more flexible management of the memory of some of the more automated.

A ES6 face questions

Introduced to distinguish Set, Map, WeakSet and WeakMap of?

  • Set:
    • Members can not be repeated
    • Only the key, no key name
    • You can traverse, methods add, delete, has
  • WeakSet:
    • Members are subject
    • Members are weak references, can disappear at any time, can be used to save the DOM node, it is not likely to cause a memory leak
  • Map:
    • It is essentially a collection of key-value pairs, similar to the set. Which may be non-string .Object key name key-value pair "key" is actually a string, and may be a key Map other types of values.
    • You can traverse,
  • WeakMap:
  • Only accept objects as keys (except the null), does not accept the values ​​of other types as keys
  • Keys points to an object, not included in garbage collection
  • Can not traverse, methods get, set, has, delete
Published 114 original articles · won praise 146 · Views 250,000 +

Guess you like

Origin blog.csdn.net/Sophie_U/article/details/103810451