Home >Web Front-end >JS Tutorial >Detailed explanation of examples of set-map data structure in ES6

Detailed explanation of examples of set-map data structure in ES6

零下一度
零下一度Original
2017-06-26 09:12:311891browse

1. Set data structure (use new to create a set collection, add elements through the add method, and obtain the length of the set collection through size)  

{
  let list = new Set();
  list.add(5);
  list.add(7);

  console.log('size',list.size);
}

 There is another way to initialize (by passing the array directly)

{
  let arr = [1,2,3,4,5];
  let list = new Set(arr);

  console.log('size',list.size);
}

 The elements in set cannot be repeated (Array deduplication can be carried out through this feature of set. Note: data type conversion will not be performed during the conversion process)

{
  let list = new Set();
  list.add(1);
  list.add(2);
  list.add(1);//不会报错 只是不会生效

  console.log('list',list);

  let arr=[1,2,3,1,'2'];
  let list2=new Set(arr);

  console.log('unique',list2);
}

  Some methods of set (add, delete, clear, has) 

{
  let arr=['add','delete','clear','has'];
  let list=new Set(arr);

  console.log('has',list.has('add'));//是否包含
  console.log('delete',list.delete('add'),list);//清空
  list.clear();
  console.log('list',list);
}

 Traversal of set (keys and values ​​return the values ​​​​in the set)

{
  let arr=['add','delete','clear','has'];
  let list=new Set(arr);  for(let key of list.keys()){
    console.log('keys',key);
  }  for(let value of list.values()){
    console.log('value',value);
  }  for(let [key,value] of list.entries()){
    console.log('entries',key,value);
  }

  list.forEach(function(item){console.log(item);})
}

 WeakSet (the elements inside can only be objects, and it will not detect whether the added object is referenced elsewhere, and traversal is not supported without size clear)

{
  let weakList=new WeakSet();

  let arg={};

  weakList.add(arg);  // weakList.add(2);
  console.log('weakList',weakList);
}

2. Map data structure (Map is set through key/value, so the set method is used for setting and the get method is used for obtaining)  

{
  let map = new Map();
  let arr=['123'];

  map.set(arr,456);

  console.log('map',map,map.get(arr));
}

 Another way to define Map (at the same time the size delete clear method is the same)

{
  let map = new Map([['a',123],['b',456]]);
  console.log('map args',map);
  console.log('size',map.size);
  console.log('delete',map.delete('a'),map);
  console.log('clear',map.clear(),map);
}

 weakMap data structure (the same characteristics as setMap)

{
  let weakmap=new WeakMap();

  let o={};
  weakmap.set(o,123);
  console.log(weakmap.get(o));
}

  

The above is the detailed content of Detailed explanation of examples of set-map data structure in ES6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn