ES6 learning notes: Comparison of map, set, array, and object; es6 learning notes
Preface
The data structure in ES5 mainly uses Array and Object. The Set and Map data structures are added in ES6. Up to now, there are four common data structures: Array, Object, Set, and Map. I won't talk much about it below. Let's take a look at the detailed introduction.
// Horizontal comparison, addition, query, modification, and deletion of data structures
1. Comparison of map and array
{Let map = new Map (); let array = [];/** add **/map. set ('T', 1); array. push ({t: 1}); console.info ('map-array', map, array);/** query **/let map_exist = map. has ('T'); let array_exist = array. find (item => item. t); console.info ('map-array', map_exist, array_exist);/** change **/map. set ('T', 2); array. forEach (item => item. t? Item. t = 2: ''); lele.info ('map-array-modify ', map, array);/** Delete **/map. delete ('T'); let index = array. findIndex (item => item. t); array. splice (index, 1); console.info ('map-array-empty', map, array );}
2. Comparison between set and array
{Let set = new Set (); let array = []; // Add set. add ({t: 1}); array. push ({t: 1}); console.info ('set-array', set, array); // query let set_exist = set. has ({t: 1}); let array_exist = array. find (item => item. t); lele.info ('set-array', set_exist, array_exist); // change set. forEach (item => item. t? Item. t = 2: ''); array. forEach (item => item. t? Item. t = 2: ''); lele.info ('set-array-modify ', set, array); // Delete set. forEach (item => item. t? Set. delete (item): ''); let index = array. findIndex (item => item. t); array. splice (index, 1); console.info ('set-array-empty', set, array );}
3. Comparison of map, set, and Object
{Let item = {t: 1}; let map = new Map (); let set = new Set (); let obj = {}; // Add map. set ('T', 1); set. add (item); obj ['T'] = 1; console.info ('map-set-obj ', obj, map, set); // query lele.info ({map_exist: map. has ('T'), set_exist: set. has (item), obj_exist: 't'in obj}) // modify map. set ('T', 2); item. t = 2; obj ['T'] = 2; console.info ('map-set-obj-modify ', obj, map, set); // Delete map. delete ('T'); set. delete (item); delete obj ['T']; console.info ('map-set-obj-empty', obj, map, set );}
Through comparison, we can find that map can be used first without arrays,
Consider data uniqueness, consider using set instead of Objet
In future development, You can prioritize map and set, and discard arrays and objects.
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.