set.md 3.2 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 93 94 95 96 97 98 99 100 101 102 103 104 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
# Set

Set 对象是值的集合,你可以按照插入的顺序迭代它的元素。Set 中的元素只会出现一次,即 Set 中的元素是唯一的。

## 实例属性


### size

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

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

<!-- UTSJSON.Set.size.returnValue -->
```ts
const set1 = new Set<Any>();

set1.add(42);
set1.add('forty two');
set1.add('forty two');

console.log(set1.size);
// expected output: 2
```
<!-- UTSJSON.Set.size.compatibility -->


## 实例方法


### add(value)

<!-- UTSJSON.Set.add.description -->

<!-- UTSJSON.Set.add.param -->

<!-- UTSJSON.Set.add.returnValue -->

```ts
const set1 = new Set<number>();
set1.add(42);
set1.add(42);
set1.add(13);
set1.forEach((item)=>{
  console.log(item);
  // expected output: 42
  // expected output: 13
})
```
<!-- UTSJSON.Set.add.compatibility -->

### clear()

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

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

<!-- UTSJSON.Set.clear.returnValue -->
```ts
const set1 = new Set<any>();
set1.add(1);
set1.add('foo');
console.log(set1.size);
// expected output: 2
set1.clear();
console.log(set1.size);
// expected output: 0
```
<!-- UTSJSON.Set.clear.compatibility -->

### delete(value)

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

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

<!-- UTSJSON.Set.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.Set.delete.compatibility -->

### forEach(callbackfn, thisArg?)

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

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

<!-- UTSJSON.Set.forEach.returnValue -->
```ts
const set1 = new Set<number>([42, 13]);
set1.forEach((item)=>{
  console.log(item);
  // expected output: 42
  // expected output: 13
})
```
<!-- UTSJSON.Set.forEach.compatibility -->

### has(value)

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

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

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

```ts
const set1 = new Set<number>([1, 2, 3, 4, 5]);

console.log(set1.has(1));
// expected output: true

console.log(set1.has(5));
// expected output: true

console.log(set1.has(6));
// expected output: false
```
<!-- UTSJSON.Set.has.compatibility -->.

<!-- UTSJSON.Set.tutorial -->

杜庆泉's avatar
杜庆泉 已提交
130
## Android 平台方法
D
DCloud_LXH 已提交
131

杜庆泉's avatar
杜庆泉 已提交
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
* 目前 Set 类型编译到 kotlin 为 io.dcloud.uts.Set


::: preview

> UTS

```ts
// 创建Kotlin HashSet
let kotlinSet = new kotlin.collections.HashSet<string>()
kotlinSet.add("a")
kotlinSet.add("b")
// 转换为 UTS Set
let utsSet = new Set<string>()
utsSet.addAll(kotlinSet)
console.log(utsSet)
// UTS Set 转换为 Kotlin HashSet
let nextKotlinSet = new kotlin.collections.HashSet<string>()
nextKotlinSet.addAll(utsSet)
console.log(nextKotlinSet)
```

> Kotlin

```Kotlin
// 创建Kotlin HashSet
var kotlinSet = kotlin.collections.HashSet<String>();
kotlinSet.add("a");
kotlinSet.add("b");
// 转换为 UTS Set
var utsSet = Set<String>();
utsSet.addAll(kotlinSet);
console.log(utsSet, " at pages/index/helloView.uvue:35");
// UTS Set 转换为 Kotlin HashSet
var nextKotlinSet = kotlin.collections.HashSet<String>();
nextKotlinSet.addAll(utsSet);
console.log(nextKotlinSet, " at pages/index/helloView.uvue:38");
```

:::