Sets_and_Maps

Sets and maps

In ex6 ,two more data structures were finally introduced.

Set is iterable

Sets

Basic usage

Use new Set() to creat a new set.

1
2
const set = new Set(['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']);
console.log(set);//Set(3) { 'a', 'b', 'c' }
1
2
const set = new Set('aniya');
console.log(set);//Set(4) { 'a', 'n', 'i', 'y' }
  • The input must be iterable.

  • And the elements in a set is unique.

  • And there is no order of the elements. No index.

So there is no need to get a value form sets.

Methods of set

  1. .size
1
2
const set //4= new Set('aniya');
console.log(set.size);//4
  1. .has()
1
2
const set = new Set('aniya');
console.log(set.has('a'), set.has('0'));//true false

To find out whether the set has the thing you want to know.

  1. .add()
1
2
3
4
const set = new Set();
console.log(set);//Set(0) {}
set.add('Tommy idiot');
console.log(set);//Set(1) { 'Tommy idiot' }

You can just add one at one time.

  1. .delete
1
2
3
4
5
6
const set = new Set();
console.log(set); //Set(0) {}
set.add('Tommy idiot');
console.log(set); //Set(1) { 'Tommy idiot' }
set.delete('Tommy idiot');
console.log(set);//Set(0) {}
  1. .clear()
1
2
3
const set = new Set('aniya');
set.clear();
console.log(set);//Set(0) {}

To clear out the set.

Loop over the set

1
2
3
4
5
6
7
8
const set = new Set('aniya');
for (const item of set) {
console.log(item);
}
//a
//n
//i
//y

Some usecases of sets

  • To remove duplicated values of arrarys.
1
2
3
const arr = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];
const arrUnique = [...new Set(arr)];
console.log(arrUnique);//[ 1, 2, 3, 4 ]

And we can konw its size by the size method.

Maps

  • Maps is alike to objects,data are also stored as key-value pairs.

  • The biggest diffrence is that keys in maps can be any type,such as number, string ,object and even another map.

  • While the keys in objects can only be string.

Creat a map

1
2
3
4
5
6
7
8
9
10
11
const question = new Map([
['key', 'value'],
['question', "What's the best programming language in the world?"],
[1, 'C'],
[2, 'Java'],
[3, 'Javascript'],
['correct', 3],
[true, 'Congratulations! 🤣'],
[false, 'Try again!😅'],
]);
console.log(question);

The argument is a big arrary which contains many small arrarys which are compose of key-value pairs.

.set()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let o = {
name: 'jonas',
};
const map = new Map();
map.set(1, 'the first');
map.set('key', 2);
map.set(o, o);
console.log(map.set(o, o));
console.log(map);
//Map(3) {
1 => 'the first',
'key' => 2,
{ name: 'jonas' } => { name: 'jonas' }
}
  • Calling the set method like this does not only update the map that’s called on,but it also returns the map.

So you can just do that like this:

1
2
3
4
5
6
7
map.set(1, 'the first').set(o, o).set('key', 2);
console.log(map);
//Map(3) {
1 => 'the first',
'key' => 2,
{ name: 'jonas' } => { name: 'jonas' }
}

.get()

1
console.log(map.get('key'));//2

.has()

.delete()

If you want to use an “Complex datatype” as an key ,you should store it in an variation first.

  • Wrong

    1
    2
    3
    const map = new Map();
    map.set([1, 2], 'this');
    console.log(map.get([1, 2]));//undefined

    Because the two [1,2] is not the same object that is stored in heap.

  • Right

    1
    2
    3
    4
    const map = new Map();
    const arr = [1, 2];
    map.set(arr, 'this');
    console.log(map.get(arr)); //this

Convert a object into a map.

1
newMap = new Map(Object.entries(objectName));

Loop over a map

1
2
3
for(const [key,value] of mapName){
console.log(key,value);
}

本站由 @Eureka 使用 Stellar 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。