Home > Web Front-end > JS Tutorial > body text

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

零下一度
Release: 2017-06-26 09:12:31
Original
1823 people have browsed it

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);
}
Copy after login

 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);
}
Copy after login

 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);
}
Copy after login

  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);
}
Copy after login

 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);})
}
Copy after login

 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);
}
Copy after login

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));
}
Copy after login

 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);
}
Copy after login

 weakMap data structure (the same characteristics as setMap)

{
  let weakmap=new WeakMap();

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

  

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!