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

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

雪洛's avatar
雪洛 已提交
5 6 7 8 9 10 11 12 13 14
**注意:请勿使用下标访问或设置map的键值对,此用法虽然在uts转为kotlin时可用,但是并不跨端**

```ts
const map = new Map<string, string>()
map['key1'] = 'value1' // 不跨端的用法
map.set('key1', 'value1') // 跨端用法
console.log(map['key1']) // 不跨端的用法
console.log(map.get('key1')) // 跨端用法
```

D
DCloud_LXH 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
## 实例属性


### size

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

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

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

```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

```

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


## 实例方法


### clear()

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

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

<!-- UTSJSON.Map.clear.returnValue -->

```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
```

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

### delete(key)

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

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

<!-- UTSJSON.Map.delete.returnValue -->

```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
```

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

### forEach(callbackfn, thisArg?)

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

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

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

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

杜庆泉's avatar
杜庆泉 已提交
93 94 95 96 97 98 99 100 101 102 103 104
```ts

let mapObj = new Map<string,any>()
mapObj.put("name","zhangsan")
mapObj.put("age",12)
// 需要特别注意迭代方法的第一个参数是value.第二个是key. 
mapObj.forEach(function(value:any,key:string){
    console.log(key)
    console.log(value)
})
```

D
DCloud_LXH 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
### get(key)

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

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

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

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

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

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

### has(key)

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

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

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

```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
```

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

### set(key, value)

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

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

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

```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
```

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

注意:由于Map的key是唯一的,给同一个key多次set值时,会用新值替换老值。
```ts
const map1: Map<string,string> = new Map(); //定义一个map,key为string类型,value也是string类型
map1.set('key1', "abc");
map1.set('key1', "def");
console.log(map1.get('key1')) //返回 def
```

## 常见操作

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

let map = new Map<string,any | null>()
map.set("name","zhangsan")
map.set("age",12)

// 遍历函数 1
map.forEach(function(value:any | null){
    console.log(value)
})
// 遍历函数 2
map.forEach(function(value:any | null,key:string){
    console.log(key)
    console.log(value)
})
// 遍历函数 3
map.forEach(function(value:any | null,key:string,map: Map<string,any | null>){
    console.log(value)
    console.log(key)
    console.log(map)
})

```

<!-- UTSJSON.Map.tutorial -->