11-Javaweb learning -ES6- collection
A, Set
Set, is a collection of a bunch of things, Set bit like Array- array, but with the array is not the same, Set there can not have duplicate content.
- Set Object creation
let desserts = new Set(['冰淇淋','蛋糕']);
- Set to add data
desserts.add("曲奇");
- Get the length of the Set
desserts.size;
- Set to determine whether to include a certain element
desserts.has("甜甜圈");
- Set remove one element
desserts.delete("蛋糕");
- Traversal Set
desserts.forEach(dessert => {console.log(dessert);})
- Empty Set
desserts.clear();
Two, Map
If we need a name-value pair data structure, you can use an object, an object which can contain multiple properties, each property has a name, and a value corresponding with him, but there will be some restrictions on using objects or cause conflict such objects can not be used as the name of the property. Now we can use this data to organize Map name-value pairs, you can create a map using the new Map ()
- Create a Map Object
let food = new Map();
- Map data add
let fruit = {}, cook = function(){}, dessert = "甜点”;
food.set(fruit,"苹果");
food.set(cook,"刀叉");
food.set(dessert,"甜甜圈");
- Gets the object in the Map
food.get(fruit);
- View Map of length
food.size;
- Map To remove an element
food.delete(dessert);
- Traversal Map
food.forEach((value,key) => {console.log(value);console.log(key);})