forked from XPoet/js-data-structure-and-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# JavaScript 数据结构与算法(八)字典 | ||
|
||
## 字典 | ||
|
||
### 字典特点 | ||
|
||
- 字典存储的是**键值对**,主要特点是**一一对应**。 | ||
- 比如保存一个人的信息 | ||
- 数组形式:`[19,‘Tom’,1.65]`,可通过下标值取出信息。 | ||
- 字典形式:`{"age":19,"name":"Tom","height":165}`,可以通过 `key` 取出 `value`。 | ||
- 此外,在字典中 key 是不能重复且无序的,而 Value 可以重复。 | ||
|
||
### 字典和映射的关系 | ||
|
||
- 有些编程语言中称这种映射关系为**字典**,如 Swift 中的 Dictonary,Python 中的 dict。 | ||
- 有些编程语言中称这种映射关系为 **Map**,比如 Java 中的 HashMap 和 TreeMap 等。 | ||
|
||
### 字典常见的操作 | ||
|
||
- `set(key,value)` 向字典中添加新元素。 | ||
- `remove(key)` 通过使用键值来从字典中移除键值对应的数据值。 | ||
- `has(key)` 如果某个键值存在于这个字典中,则返回 `true`,反之则返回 `false`。 | ||
- `get(key)` 通过键值查找特定的数值并返回。 | ||
- `clear()` 将这个字典中的所有元素全部删除。 | ||
- `size()` 返回字典所包含元素的数量。与数组的 `length` 属性类似。 | ||
- `keys()` 将字典所包含的所有键名以数组形式返回。 | ||
- `values()` 将字典所包含的所有数值以数组形式返回。 | ||
|
||
### 字典封装 | ||
|
||
#### 代码实现 | ||
|
||
```js | ||
// 字典结构的封装 | ||
export default class Map { | ||
constructor() { | ||
this.items = {}; | ||
} | ||
|
||
// has(key) 判断字典中是否存在某个 key | ||
has(key) { | ||
return this.items.hasOwnProperty(key); | ||
} | ||
|
||
// set(key, value) 在字典中添加键值对 | ||
set(key, value) { | ||
this.items[key] = value; | ||
} | ||
|
||
// remove(key) 在字典中删除指定的 key | ||
remove(key) { | ||
// 如果集合不存在该 key,返回 false | ||
if (!this.has(key)) return false; | ||
delete this.items[key]; | ||
} | ||
|
||
// get(key) 获取指定 key 的 value,如果没有,返回 undefined | ||
get(key) { | ||
return this.has(key) ? this.items[key] : undefined; | ||
} | ||
|
||
// 获取所有的 key | ||
keys() { | ||
return Object.keys(this.items); | ||
} | ||
|
||
// 获取所有的 value | ||
values() { | ||
return Object.values(this.items); | ||
} | ||
|
||
// size() 获取字典中的键值对个数 | ||
size() { | ||
return this.keys().length; | ||
} | ||
|
||
// clear() 清空字典中所有的键值对 | ||
clear() { | ||
this.items = {}; | ||
} | ||
} | ||
``` | ||
|
||
#### 代码测试 | ||
|
||
```js | ||
const map = new Map(); | ||
|
||
// set() 测试 | ||
map.set("name", "XPoet"); | ||
map.set("age", 18); | ||
map.set("email", "i@xpoet.cn"); | ||
console.log(map); // {items: {name: "XPoet", age: 18, email: "i@xpoet.cn"}} | ||
|
||
// has() 测试 | ||
console.log(map.has("name")); //--> true | ||
console.log(map.has("address")); //--> false | ||
|
||
// remove() 测试 | ||
map.remove("name"); | ||
console.log(map); // {age: 18, email: "i@xpoet.cn"} | ||
|
||
// get() 测试 | ||
console.log(map.get("age")); //--> 18 | ||
|
||
// keys() 测试 | ||
console.log(map.keys()); //--> ["age", "email"] | ||
|
||
// values() 测试 | ||
console.log(map.values()); //--> [18, "i@xpoet.cn"] | ||
|
||
// size() 测试 | ||
console.log(map.size()); //--> 2 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Map from './map'; | ||
|
||
// ---------------- 封装的字典结构测试 ---------------- // | ||
console.log('// ----- 字典结构测试 START -----//'); | ||
const map = new Map(); | ||
|
||
// set() 测试 | ||
map.set('name', 'XPoet'); | ||
map.set('age', 18); | ||
map.set('email', 'i@xpoet.cn'); | ||
console.log(map); // {items: {name: "XPoet", age: 18, email: "i@xpoet.cn"}} | ||
|
||
// has() 测试 | ||
console.log(map.has('name')); //--> true | ||
console.log(map.has('address')); //--> false | ||
|
||
// remove() 测试 | ||
map.remove('name'); | ||
console.log(map); // {age: 18, email: "i@xpoet.cn"} | ||
|
||
// get() 测试 | ||
console.log(map.get('age')); //--> 18 | ||
|
||
// keys() 测试 | ||
console.log(map.keys()); //--> ["age", "email"] | ||
|
||
// values() 测试 | ||
console.log(map.values()); //--> [18, "i@xpoet.cn"] | ||
|
||
// size() 测试 | ||
console.log(map.size()); //--> 2 | ||
|
||
console.log('// ----- 字典结构测试 END -----//'); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 字典结构的封装 | ||
export default class Map { | ||
|
||
constructor() { | ||
this.items = {}; | ||
} | ||
|
||
// has(key) 判断字典中是否存在某个 key | ||
has(key) { | ||
return this.items.hasOwnProperty(key); | ||
} | ||
|
||
// set(key, value) 在字典中添加键值对 | ||
set(key, value) { | ||
this.items[key] = value; | ||
} | ||
|
||
// remove(key) 在字典中删除指定的 key | ||
remove(key) { | ||
// 如果集合不存在该 key,返回 false | ||
if (!this.has(key)) return false; | ||
delete this.items[key]; | ||
} | ||
|
||
// get(key) 获取指定 key 的 value,如果没有,返回 undefined | ||
get(key) { | ||
return this.has(key) ? this.items[key] : undefined; | ||
} | ||
|
||
// 获取所有的 key | ||
keys() { | ||
return Object.keys(this.items); | ||
} | ||
|
||
// 获取所有的 value | ||
values() { | ||
return Object.values(this.items); | ||
} | ||
|
||
// size() 获取字典中的键值对个数 | ||
size() { | ||
return this.keys().length; | ||
} | ||
|
||
// clear() 清空字典中所有的键值对 | ||
clear() { | ||
this.items = {}; | ||
} | ||
|
||
} |