11-Javaweb learning -ES6- collection

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.

  1. Set Object creation
    let desserts = new Set(['冰淇淋','蛋糕']);
  2. Set to add data
    desserts.add("曲奇");
  3. Get the length of the Set
    desserts.size;
  4. Set to determine whether to include a certain element
    desserts.has("甜甜圈");
  5. Set remove one element
    desserts.delete("蛋糕");
  6. Traversal Set
    desserts.forEach(dessert => {console.log(dessert);})
  7. 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 ()

  1. Create a Map Object
    let food = new Map();
  2. Map data add
let fruit = {}, cook = function(){}, dessert = "甜点”;
food.set(fruit,"苹果");
food.set(cook,"刀叉");
food.set(dessert,"甜甜圈");
  1. Gets the object in the Map
    food.get(fruit);
  2. View Map of length
    food.size;
  3. Map To remove an element
    food.delete(dessert);
  4. Traversal Map
    food.forEach((value,key) => {console.log(value);console.log(key);})
Published 49 original articles · won praise 9 · views 1427

Guess you like

Origin blog.csdn.net/weixin_42401546/article/details/105185752