This is the 12th day of my participation in Gwen Challenge.

Introduction to 👽

Number, String, Boolean, Object, etc. Yes, these are the two big data types in JS. In addition, there are many built-in objects in JS. As we are familiar with Array, Math, Function, etc., there are also a lot of things that we do not often use, such as Set, Map, WeakSet, WeakMap. These objects are uncommon, but not useless. Take a look!

👽 Set

👻 Basic Introduction

Set is very similar to Aaary in that it is an ordered reference object. The biggest difference between a Set and an Array is that the values in a Set cannot be repeated, whereas an Array does not.

const myArray = [1.1.2.2]

const mySet = new Set(myArray)
console.log(mySet2) Set(2) {1,2}
Copy the code

👻 commonly used API

// Get the Set length
mySet.size
// Add value to Set
mySet.add(3)
// Delete values from Set
mySet.delete(3)
/ / traverse the Set
for (let item of mySet){
 console.log(item)
}
Copy the code

👻 Tips

Since a Set has the unique property of an inner value, it is perfectly suitable for repealing.

// Array decrement
let myArray = [1.1.2.2]

myArray = Array.from(new Set(myArray))
console.log(myArray) / / output [1, 2]

// Unduplicate the string
let myStr = 'oovbyyu'

myStr = Array.from(new Set(myStr)).join(' ')
console.log(myStr) / / output ovbyu ""
Copy the code

👽 Map

👻 Basic Introduction

Maps and Objects are similar in that they are key-value pairs. The main differences are: 1. The values inside the Map are ordered (in the same order as when they were inserted); 2. Map key types are unlimited and can be any type (including functions, objects, etc.).

let myMap = new Map(a);let arrayA = [1.2]

myMap.set(arrayA,2)

console.log(myMap) Map(1) {Array(2) => 2}
Copy the code

👻 commonly used API

// Get the Map length
myMap.size
// Add value to Map
myMap.set('mapKey'.'mapVal')
// Delete values from Map
myMap.delete('mapKey')// Return true on success and false on failure
// Get the value of a key in the Map
myMap.get(arrayA)
/ / traverse Map
for (let item of myMap){
 console.log(item)
}
Copy the code

👻 Tips

Order of values in a Map is very important. If you need to ensure that the order of traversal is consistent when traversing an Object, you can use a Map.

👽 epilogue

Practice is the mother of wisdom. Try more