Structured composition and access to data is a key part of any JavaScript program. This language
From the beginning until now, the main mechanism for creating data structures has been arrays and objects. Of course, based on what they have built
Many advanced data structures have been established as user libraries.
1.TypedArray
The TypedArray object describes the array like graph of the underlying binary data buffer. There is no named TypedArray global property, and there is no directly visible TypedArray constructor. Instead, there are many different global attributes whose values are typed array constructors for specific element types.
new TypedArray(); // new in ES2017 new TypedArray(length); new TypedArray(typedArray); new TypedArray(object); new TypedArray(buffer [, byteOffset [, length]]); where TypedArray() is one of: Int8Array(); Uint8Array(); Uint8ClampedArray(); Int16Array(); Uint16Array(); Int32Array(); Uint32Array(); Float32Array(); Float64Array();
- Length when called with the length parameter, an internal array buffer is created in memory, the length of its size multiplied by BYTES_PER_ELEMENT bytes, containing zeros.typedArray.
- When called with the typedArray parameter, the parameter can be an object of any typed array type (such as Int32Array), and the typedArray is copied to a new type array. Each value is converted to the corresponding constructor type before typedArray is copied to the new array. The new typedArray object will then be the same length as the typedArray parameter.
- When the object is called with parameters, a new type array will be created, such as method.buffer, byteOffset, lengthWhen, a, and the optional a and atypedarray. From() buffer byteOffset length parameters, Create a new type array view to view the specified ArrayBuffer. The byteOffset and length parameters specify the storage range to be exposed through the type array view. If both are ignored, all buffers are viewed; If only length is omitted, buffer looks at the rest.
2. Map
let obj = {}; let shop1 = { name: 'Dalang pancake' }; let shop2 = { name: 'Rabbit moon cake' }; obj[shop1] = "Clay oven rolls"; obj[shop2] = "Moon Cake"; console.log(obj) console.log(obj[shop1]) console.log(obj[shop2])
Both outputs are moon cakes. This is because the stringing of the two objects is "[object Object]", so only one key is set.
Result: Obj = > {[object object]: "moon cake"}
Map data structure using ES6
let map = new Map(); let shop1 = { name: 'Dalang pancake' }; let shop2 = { name: 'Rabbit moon cake' }; map.set(shop1, 'Clay oven rolls'); map.set(shop2, 'Moon Cake'); console.log(map.get(shop1)); console.log(map.get(shop2)); // Baked cake, moon cake
The key is assigned two consecutive times, and the value of the last time overwrites the value of the previous time
map.set(shop2, 'olive nut moon-cake') // Shaobing, Wuren moon cake
Get the value in the map and use the get method. When it does not exist in the map, it will return undefined
map.get('shop3') // undefined
To delete an item, use the delete method
map.delete(shop2)
Clear all, using the clear method
map.clear()
Map key
let map = new Map(); let shop1 = { name: 'Dalang pancake' }; let shop2 = { name: 'Rabbit moon cake' }; map.set(shop1, ['Clay oven rolls', 'Maotai cake']); map.set(shop2, 'Moon Cake'); let keys = [...map.keys()]; let values = [...map.values()]; let entries = [...map.entries()]; console.log(keys) console.log(values); console.log(entries);
To determine whether there is a given key in a map, you can use the has(...) method
map.has(shop1)
Any data structure that has an Iterator interface and each member is a two element array can be used as a parameter of the Map constructor
const foodMap = new Map([ ['name', 'Candy bubble house'], ['address', 'Candy Street'], ['tel', 5201314] ]);
Actually
const item = [ ['name', 'Candy bubble house'], ['address', 'Candy Street'], ['tel', 5201314] ]; const foodMap = new Map(); item.forEach( ([key, value]) => foodMap.set(key, value) ); console.log(foodMap)
Set
let foodSet = new Set(); let shop1 = { name: 'Bear biscuit house' }; let shop2 = { name: 'Rainbow marshmallow' }; foodSet.add(shop1); foodSet.add(shop2); foodSet.add(shop1); console.log(foodSet.has(shop2)); console.log(foodSet.size); foodSet.delete(shop2); console.log(foodSet.size); console.log(foodSet.clear());