1: Map method

/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Map method = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / / / / / class object and object is the difference between: Object's key must be a string, Map keys to say any type let arr1 = [[' name ', 'li bai'], [' professional ', 'poets'], [false,' is a Boolean value], [[], 'is an array of']] let m = new Map (arr1). / / the console log (m) / / Map {' name '= >' li ', 'professional' = > 'poets' false = >' is a Boolean value, [] => 'array'} for (let [key,value] of m) {// console.log(' key: ',key) // console.log(' value: ',value)}Copy the code

Note:

  • Map method:
    • map.has(keyStr)Is used to determine whether attributes exist in the mapkeyStr, the return value is a Boolean
    • map.set(keyStr,itemVal).keyStrIs a property,itemValIs the value, theitemValSave to map
    • map.delete('keyStr'), delete thekeyforkeyStrThe data of
    • map.get('keyStr')To obtainkeyforkeyStrThe data of
    • map.values()To obtainmapWorth set, pass[...map.values()]A collection of values is returned to an array
  • Differences between a Map and a common object
    • MapProperties can be arbitrary values, and ordinary objects can only be strings
    • MapTo distinguish the characters'a'And lettersa, ordinary objects do not distinguish, the following value will overwrite the previous value

    Such as:

    / / common object obj = {a: 'I am a letter a', 'a' : 'I am a string of 1:' ordinary employees, 2: 'boss', 1: 'administrators',' 2 ', 'VIP customers'} the console. The log (obj) / / {1: "Admin ",2: "VIP customer ", A:" I am string A "}Copy the code

    Map.set (a,' I am the letter A ')Error will be reported, need to changeMap.set ('a',' I am string a')

Two: Set() method

/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Set method = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * / / / / / class array and array is the difference between: Array is [] surrounded, Set is {} surrounded, Let s = new Set(arr2) // console.log(s) // Set {1,2,3, 4, 4, 5} // instance 1: Let temp21 = [1,2,4,2,3,1,5] let temp22 = [1,4,4,2,10,1,9 Set(temp21) let tempArr22 = new Set(temp22) return Array.from(tempArr21).filter(item => { return tempArr22.has(item) }) } // Example 2: Function unBothHas(temp21,temp22) {let tempArr21 = new Set(temp21) let tempArr22 = new Set(temp22) return function unBothHas(temp21,temp22) {let tempArr21 = new Set(temp22) return Array.from(tempArr21).filter(item => { return ! tempArr22.has(item) }) } console.log('data2==',bothHas(temp21,temp22)) console.log('data3==',unBothHas(temp21,temp22))Copy the code

Note:

  • The Set method:
    • set.add(val)To add value
    • set.delete(val), delete the value
    • set.has(val), determine if there isvalvalue
  • Set attributes:
    • set.sizeReturns the number of elements contained in the collection