map.md 2.9 KB
Newer Older
D
DCloud_LXH 已提交
1
# Map
D
DCloud_LXH 已提交
2 3 4

Map 对象保存键值对。任何值(对象或者基本类型)都可以作为一个键或一个值。

D
DCloud_LXH 已提交
5 6
## 实例属性

D
DCloud_LXH 已提交
7 8 9

### size

D
DCloud_LXH 已提交
10 11 12 13 14
<!-- UTSJSON.Map.size.description -->

<!-- UTSJSON.Map.size.param -->

<!-- UTSJSON.Map.size.returValue -->
D
DCloud_LXH 已提交
15 16 17 18 19 20 21 22 23 24 25

```ts
const map1 = new Map<string,string>();
map1.set('a', 'alpha');
map1.set('b', 'beta');
map1.set('g', 'gamma');
console.log(map1.size);
// expected output: 3

```

D
DCloud_LXH 已提交
26 27 28 29 30 31 32
<!-- UTSJSON.Map.size.compatibility -->


## 实例方法


### clear()
D
DCloud_LXH 已提交
33

D
DCloud_LXH 已提交
34
<!-- UTSJSON.Map.clear.description -->
D
DCloud_LXH 已提交
35

D
DCloud_LXH 已提交
36 37 38
<!-- UTSJSON.Map.clear.param -->

<!-- UTSJSON.Map.clear.returValue -->
D
DCloud_LXH 已提交
39 40 41 42 43 44 45 46 47 48 49 50

```ts
const map1 = new Map<string,string>();
map1.set('bar', 'baz');
map1.set(1, 'foo');
console.log(map1.size);
// expected output: 2
map1.clear();
console.log(map1.size);
// expected output: 0
```

D
DCloud_LXH 已提交
51 52 53 54 55
<!-- UTSJSON.Map.clear.compatibility -->

### delete(key)

<!-- UTSJSON.Map.delete.description -->
D
DCloud_LXH 已提交
56

D
DCloud_LXH 已提交
57 58 59
<!-- UTSJSON.Map.delete.param -->

<!-- UTSJSON.Map.delete.returValue -->
D
DCloud_LXH 已提交
60 61 62 63 64 65 66 67 68 69 70

```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');
console.log(map1.delete('bar'));
// expected result: true
// (true indicates successful removal)
console.log(map1.has('bar'));
// expected result: false
```

D
DCloud_LXH 已提交
71 72 73 74 75
<!-- UTSJSON.Map.delete.compatibility -->

### forEach(callbackfn, thisArg?)

<!-- UTSJSON.Map.forEach.description -->
D
DCloud_LXH 已提交
76

D
DCloud_LXH 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
<!-- UTSJSON.Map.forEach.param -->

<!-- UTSJSON.Map.forEach.returValue -->

<!-- UTSJSON.Map.forEach.compatibility -->

### get(key)

<!-- UTSJSON.Map.get.description -->

<!-- UTSJSON.Map.get.param -->

<!-- UTSJSON.Map.get.returValue -->
D
DCloud_LXH 已提交
90 91 92 93 94 95 96 97 98

```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');

console.log(map1.get('bar'));
// expected output: "foo"
```

D
DCloud_LXH 已提交
99 100 101
<!-- UTSJSON.Map.get.compatibility -->

### has(key)
D
DCloud_LXH 已提交
102

D
DCloud_LXH 已提交
103 104 105 106 107
<!-- UTSJSON.Map.has.description -->

<!-- UTSJSON.Map.has.param -->

<!-- UTSJSON.Map.has.returValue -->
D
DCloud_LXH 已提交
108 109 110 111 112 113 114 115 116 117 118 119

```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');

console.log(map1.has('bar'));
// expected output: true

console.log(map1.has('baz'));
// expected output: false
```

D
DCloud_LXH 已提交
120 121 122
<!-- UTSJSON.Map.has.compatibility -->

### set(key, value)
D
DCloud_LXH 已提交
123

D
DCloud_LXH 已提交
124 125 126 127 128
<!-- UTSJSON.Map.set.description -->

<!-- UTSJSON.Map.set.param -->

<!-- UTSJSON.Map.set.returValue -->
D
DCloud_LXH 已提交
129 130 131 132 133 134 135 136 137 138 139

```ts
const map1 = new Map<string,string>();
map1.set('bar', 'foo');

console.log(map1.get('bar'));
// expected output: "foo"

console.log(map1.get('baz'));
// expected output: null
```
杜庆泉's avatar
杜庆泉 已提交
140

D
DCloud_LXH 已提交
141 142 143
<!-- UTSJSON.Map.set.compatibility -->

## 常见操作
杜庆泉's avatar
杜庆泉 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

- 创建map
```ts
let map = new Map<string,any>()
map.put("name","zhangsan")
map.put("age",12)
//Map(2) {"name":"zhangsan","age":12} 
console.log(map)
```
- 通过key访问map元素
```ts
let map = new Map<string,any>()
map.put("name","zhangsan")
map.put("age",12)
let nameVal = map['name']
//zhangsan
console.log(nameVal)
```
- 遍历map
```ts
let map = new Map<string,any>()
map.put("name","zhangsan")
map.put("age",12)
map.forEach(function(key:string,value:any){
    console.log(key)
    console.log(value)
})
```