Data Structure Part II

4-3 collection

Set (set) in ES6 is a data structure introduced in, for indicating a set of unique values, so it can not contain duplicate values. Take down this section, let us look at this new specific data structures.

4-3-1 What is the set

In ES6 standards before, an optional limited data structure types, we can say only this array data structure. And the array used is a numerical index, and thus are often used to simulate the behavior of the queue and stack. But if you need to use non-numeric index, it will create the required data structures with non-array objects, and this is the Map Set behind a set of to be introduced early maps of real cash.

Set is a set of non-duplicate element in the list, which is the biggest feature of this data structure.

4-3-2 Create Collection

To create a collection that is simple, direct use can create a new Set object. If you want to create a collection when it contains the initial value, then we can pass an array inside.

let s1 = new Set();

let s2 = new Set([1,2,3]);

console.log(s1);//Set {}

console.log(s2);//Set { 1, 2, 3 }

4-3-3 for the collection added value

Use add () method can add value to a collection, because the call to add () method returns again after a Set object that with our continuous calls add () method to add value, the same as the chain of method calls approach is called a chain transfer with.

let s1 = new Set();

s1.add(1);

console.log(s1);//Set { 1 }

s1.add(2).add(3).add(4);

console.log(s1);

The Set // {. 1, 2,. 3,}. 4 we can directly pass an array which add () method

let s1 = new Set();

s1.add([1,2,3]);

console.log(s1);

//Set { [ 1, 2, 3 ] }

However, it notes that when creating objects Set incoming calls with an array of add () passed an array when the effect is not the same method, except as follows:

An element of the array passed in the establishment Set objects, each item in the array of objects to be Set

let s1 = new Set([1,2,3]);

console.log(s1);//Set { 1, 2, 3 } console.log(s1.size);//3

An element of the array when the incoming call add () method, the object is as Set

let s1 = new Set();

s1.add([1,2,3]);

console.log(s1);//Set { [ 1, 2, 3 ] } console.log(s1.size);//1

Set in the object, it is not possible to add the same element, which is a very important characteristic

let s1 = new Set();

s1.add(1).add(2).add(2).add(3);

console.log(s1);

//Set { 1, 2, 3 }

4-3-4 set of properties and methods

  1. By size obtaining the number of element properties

let s1 = new Set([1,2,3]);

console.log(s1.size);//3

  1. Use hasO method to see whether the collection contains a certain value

let s1 = new Set([1,2,3]);

console.log(s1.has(1));//true


  1. Delete a set of values

Use delete to delete Set a certain element objects inside

let s1 = new Set([1,2,3]);

s1.delete(2);

console.log(s1);//Set { 1, 3 }

// no elements are also not being given

s1.delete("2");

console.log(s1);//Set { 1, 3 }

If you want to delete all the elements of a one-time, you can use the clear method

let s1 = new Set([1,2,3]);

s1.clear()

console.log(s1);//Set {}

4-3-5 through the collection

Collection also can enumerate, we can also use for-o f to traverse the collection, as follows :

let s = new Set([1,2,3,4,5]);

for(let i of s){ console.log(i);

}

// 1

// 2

// 3

// 4

// 5

Or by for Each be traversed, for example:

// use forEach traverse

let s = new Set([1,2,3,4,5]); s.forEach(ele => console.log(ele));

// 1

// 2


// 4

// 5 In addition, we can also use the set which comes with keys (), values () and ent ries () method of collection to traverse. Say what is the way in which a collection of keys and values are the same.

keys () key collection method walks

let s = new Set(["Bill","Lucy","David"]); for(let i of s.keys()){

console.log(i);

}

// Bill

// Lucy

// David

values () values of a set of traversal method

let s = new Set(["Bill","Lucy","David"]); for(let i of s.values()){

console.log(i);

}

// Bill

// Lucy

// David

ent ries () keys and values set Simultaneous traversal

let s = new Set(["Bill","Lucy","David"]); for(let i of s.entries()){

console.log(i);

}

// [ 'Bill', 'Bill' ]

// [ 'Lucy', 'Lucy' ]

// [ 'David', 'David' ]

4-3-6 collections transfer array

Set into the array, the fastest way is to use the previously spoken extended operator as follows :

let s1 = new Set([1,2,3]);

console.log(s1);//Set { 1, 2, 3 }

let arr = [...s1]; console.log(arr);//[ 1, 2, 3 ]

In addition, we can also use Arra y supplied object from . Method to convert

let s1 = new Set([1,2,3]); console.log(s1);//Set { 1, 2, 3 }

let arr = Array.from(s1); console.log(arr);//[ 1, 2, 3 ]

We have mentioned earlier, the Set objects which are not capable of storing the same elements, use of this feature, we can quickly the array deduplication, as follows:

let arr = [1,2,2,3,4,3,1,6,7,3,5,7];

let s1 = new Set(arr);

let arr2 = [...s1]; console.log(arr2);//[ 1, 2, 3, 4, 6, 7, 5 ]

4-3-7 weak set (extension)

When the object is added to the collection, as long as the present collection, which has been stored in the set. Even if the reference object is also deleted according to Ran So, we look at the following example:

let arr = [1,2,3];

let s = new Set(arr);

= arr null; // delete arr points to an array of

the console.log (S); // {the Set . 1, 2,. 3 } array still exist in the collection  the console.log (ARR); // null

We can see, here we delete the reference to the array, but the array still exists, but the value inside the n-ULL , so the case would not have garbage collection will not recover this array, which may cause a memory leak

What is a memory leak?

- When a program which retains its value can no longer be accessed in memory, a memory leak occurs, with no space that is occupied, resulting in a waste of memory.

For example :

let arr = [1,2,3]; arr = null;

Disconnect the arr of 1, 2, 3 references, now 1, 2, 3 in the memory which is already garbage. Memory leaks will gradually reduce all available memory, resulting in a slower speed and system crashes or even

So how we can empty these useless data? For example, in the above example 1, 2, 3 . In fact, in JavaScript in it uses dynamic memory management techniques, such as garbage collection, automatically delete what is no longer needed program from memory. And some compiled programming languages, such as C ++ , the programmer is required to manually manage memory, something after the completion of the task, it is from memory deleted.

So, the problem is that even if the loss of a set of references, it will not be garbage collected, this time we can use a weak collection to avoid this situation. Create a set of weak using new operator and WeakSet ()

let weak = new WeakSet();

You can not add a weak set of basic data types, that is not so simple to add value to it like this collection.

let weak = new WeakSet();

weak.add(1);

//TypeError: Invalid value used in weak set

In addition to this limitation, weak collections and general collections there are some subtle differences, for example, can not be passed when creating a weak set of array initialization. But the collection also has a weak has (), add (), delete () methods.

let arr = [1,2,3,4,5];

let weak = new WeakSet(arr);

//TypeError: Invalid value used in weak set

// can not pass an array is initialized when you create a set of weak

Also point to note is that the collection is weak weak reference to the object, so you can not access the list of values objects inside. This makes the weak set together look like empty, but not empty, prove the following:

let weak = new WeakSet();

let arr = [1,2,3];

weak.add(arr);

console.log(weak);//WeakSet {}

console.log(weak.has(arr));//true

4-4 Mapping

Mapping (Map) is ES6 a data structure introduced specification. This is a store key - value of the list is a convenient method, similar to other programming languages dictionary or hash table. This section, let us look at this map data structure.

4-4-1 What is the mapping

JavaScript objects (Object) , is essentially a collection of key-value pairs (Hash structure ), but traditionally only use strings as keys. This gives its use is a big limitation. Russian ladies will be the sixth chapter to learn JavaScript inside an object )

To solve this problem, ES6 provides a Map data structure. It is similar to the object, but also a set of key-value pairs, but the scope of the "key" is not limited to character string, various kinds of values (including objects) can be used as keys. That is, Object structure (target structure) Providing "GOD value corresponding to a string, and Map structure provides a " value corresponds to a value of GOD, a better H ash solid structure now.

4-4-2 Creating maps

Using the new keyword and Map () constructor, you can create an empty m ap target. If you want to Map add map new elements, you can call set () method and were passing key names and corresponding values as two parameters. If you want to get the collection of information, you can call get () method.

let m = new Map();

m.set("name","xiejie");

m.set("age",18);

console.log(m);

//Map { 'name' => 'xiejie', 'age' => 18 }

console.log(m.get("name"));

// xiejie

In the object, the object can not be used as keys object properties. But in the Map map, but we can do it, so to speak, in the Map can use any data type as the key mapping inside.

let m = new Map();

m.set({},"xiejie"); m.set([1,2,3],18);

m.set(3581,18);

console.log(m);

//Map { {} => 'xiejie', [ 1, 2, 3 ] => 18, 3581 => 18 }

Incoming array to initialize Map mapping can assure Map pass an array of constructor to initialize Map mapping, which is the same with Set group similar. Each array element is a sub-array, the sub-array contains keys and key values of two elements of a pair. Thus, the entire Map map contains all such two-dimensional array of two elements

let arr = [["name","xiejie"],["age",18]];

let m = new Map(arr);

console.log(m);

//Map { 'name' => 'xiejie', 'age' => 18 }

4-4-3 mapping properties and methods

In designing the new language standard, the Commission for the Map Mapping and Set collections designed the following 3 general-purpose method has (key): detect a specified key in the Map already exists maps delete (key): from Map Removes the specified map keys and their corresponding values

the Clear . : Remove M ap map of all key-value pairs

Map mapping also supports size property, which represents the current collection contains the number of key-value pairs

let arr = [["name","xiejie"],["age",18]]; let m = new Map(arr);

console.log(m);//Map { 'name' => 'xiejie', 'age' => 18 } console.log(m.size);//2 console.log(m.has("name"));//true console.log(m.get("name"));//xiejie m.delete("name");

console.log(m);//Map { 'age' => 18 } m.clear();

console.log(m);//Map {}

4-4-4 mapping traversal

Like the set, the mapping also can enumerate, it can be used to traverse the set in a similar manner. Use for-of traversing map

let m = new Map([["name","xiejie"],["age",18]]); for(let i of m){

console.log(i);

}

// [ 'name', 'xiejie' ]

// [ 'age', 18 ]

keys () key mapping method traversal

let m = new Map([["name","xiejie"],["age",18]]); for(let i of m.keys()){

console.log(i);

}

// name

// age

values () value mapping method traversal

let m = new Map([["name","xiejie"],["age",18]]); for(let i of m.values()){

console.log(i);

}

// xiejie

// 18

ent ries () key mapping Simultaneous traversal value

let m = new Map([["name","xiejie"],["age",18]]); for(let i of m.entries()){

console.log(i);

}

// [ 'name', 'xiejie' ]

// [ 'age', 18 ]

4-4-5 mapped into an array

Map structures into an array structure, or the use of relatively rapid method described earlier extended operator ... .

let arr = [["name","xiejie"],["age",18]];

let m = new Map(arr);

console.log([...m.keys()]);//[ 'name', 'age' ]

console.log([...m.values()]);//[ 'xiejie', 18 ]

console.log([...m.entries()]);//[ [ 'name', 'xiejie' ], [ 'age', 18 ] ]

the console.log ([... m]); // [ [ 'name', 'xiejie' ], [ 'Age', 18 is]] or by using Array object from . method

let arr = [["name","xiejie"],["age",18]];

let m = new Map(arr);

console.log(Array.from(m));

// [[ 'name', 'xiejie' ], [ 'Age', 18 is]]

4-4-6 weak Mapping (extended)

Weak Weak mapping and the like are set, the main problem is the garbage inside solve the mapping, the mapping used to create a weak new operator and WeakMap () Constructor

let weakMap = new WeakMap();

Weak general mapping and mapping the same, likewise has has (), get (), set (), delete () or the like.

  1. The so-called data structures, is the way computers store and organize data. Mainly refers to the data structure stored in what the computer inside.
  2. Array is the most common type of data structure. In JavaScript to create an array which can be two kinds of literal and constructor to be created.
  3. After that we can create an array such as assignments, access, and delete operations on the array.
    1. Deconstruction refers to a complex type of data into common type of data, the JavaScript may arrays and objects deconstruction.
    2. JavaScript does not support multidimensional arrays, but we can take advantage of the characteristics of dynamic languages simulate multidimensional arrays.
    3. ES6 newly added extended operator can be quickly removed for each of the objects may be iterative.
    4. Array also has many properties and methods, to master these properties and methods, it allows us to do more with less time programming.
    5. Set (the Set) is ES6 a data structure newly introduced, it is the biggest feature can not contain duplicate values.

9 • map (Map) is ES6 a data structure newly introduced, which is a key structure, characterized in that the biggest key can be any type of data.

Guess you like

Origin www.cnblogs.com/jrzqdlgdx/p/11350786.html