array.md 23.2 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4
# Array

Array 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。

杜庆泉's avatar
杜庆泉 已提交
5 6
## 构造函数

D
DCloud_LXH 已提交
7
### new \<T>(...items : T\[]) : T[]@Constructor(...items)
8 9 10 11 12 13 14

<!-- UTSJSON.Array.Constructor.description -->

<!-- UTSJSON.Array.Constructor.param -->

<!-- UTSJSON.Array.Constructor.returnValue -->

D
DCloud_LXH 已提交
15 16
<!-- UTSJSON.Array.Constructor.test -->

17 18 19 20
<!-- UTSJSON.Array.Constructor.compatibility -->

<!-- UTSJSON.Array.Constructor.tutorial -->

杜庆泉's avatar
杜庆泉 已提交
21 22 23 24 25 26 27 28 29 30 31
::: warning 注意事项

与JS中的`Array` 不同,`UTS`不支持的指定长度初始化Array的写法

```ts
let arr = new Array(10)
```

上面的代码在不同的平台的表现有差异:

- web平台
D
DCloud_LXH 已提交
32

杜庆泉's avatar
杜庆泉 已提交
33 34 35
	一个长度为10的数组,每一个元素都是 undefined

- Android/ios平台
D
DCloud_LXH 已提交
36

杜庆泉's avatar
杜庆泉 已提交
37 38 39 40 41
	一个长度为1的数组,其元素为 数字10

:::


42
## 实例属性
D
DCloud_LXH 已提交
43 44 45 46 47 48 49 50 51

### length

<!-- UTSJSON.Array.length.description -->

<!-- UTSJSON.Array.length.param -->

<!-- UTSJSON.Array.length.returnValue -->

M
mahaifeng 已提交
52
<!-- UTSJSON.Array.length.test -->
D
DCloud_LXH 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65

<!-- UTSJSON.Array.length.compatibility -->

边界情况说明:

- 在不同平台上,数组的长度限制不同,超出限制会导致相应的错误或异常
  * 编译至 JavaScript 平台时,最大长度为 2^32 - 1,超出限制会报错:`Invalid array length`
  * 编译至 Kotlin 平台时,最大长度受系统内存的限制,超出限制报错:`java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM`
  * 编译至 Swift 平台时,最大长度也受系统内存的限制,目前超出限制没有返回信息。


## 实例方法

D
DCloud_LXH 已提交
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
### toString()

<!-- UTSJSON.Array.toString.description -->

<!-- UTSJSON.Array.toString.param -->

<!-- UTSJSON.Array.toString.returnValue -->

<!-- UTSJSON.Array.toString.test -->

<!-- UTSJSON.Array.toString.compatibility -->

<!-- UTSJSON.Array.toString.tutorial -->

### add(item)

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

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

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

<!-- UTSJSON.Array.add.test -->

<!-- UTSJSON.Array.add.compatibility -->

<!-- UTSJSON.Array.add.tutorial -->

### toLocaleString()

<!-- UTSJSON.Array.toLocaleString.description -->

<!-- UTSJSON.Array.toLocaleString.param -->

<!-- UTSJSON.Array.toLocaleString.returnValue -->

<!-- UTSJSON.Array.toLocaleString.test -->

<!-- UTSJSON.Array.toLocaleString.compatibility -->

<!-- UTSJSON.Array.toLocaleString.tutorial -->

### joinToString(separator)

<!-- UTSJSON.Array.joinToString.description -->

<!-- UTSJSON.Array.joinToString.param -->

<!-- UTSJSON.Array.joinToString.returnValue -->

<!-- UTSJSON.Array.joinToString.test -->

<!-- UTSJSON.Array.joinToString.compatibility -->

<!-- UTSJSON.Array.joinToString.tutorial -->
D
DCloud_LXH 已提交
121 122 123 124 125 126 127 128 129

### find(predicate, thisArg?)

<!-- UTSJSON.Array.find.description -->

<!-- UTSJSON.Array.find.param -->

<!-- UTSJSON.Array.find.returnValue -->

M
mahaifeng 已提交
130
<!-- UTSJSON.Array.find.test -->
D
DCloud_LXH 已提交
131 132 133

<!-- UTSJSON.Array.find.compatibility -->

D
DCloud_LXH 已提交
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
### find(predicate, thisArg?)

<!-- UTSJSON.Array.find_1.description -->

<!-- UTSJSON.Array.find_1.param -->

<!-- UTSJSON.Array.find_1.returnValue -->

<!-- UTSJSON.Array.find_1.test -->

<!-- UTSJSON.Array.find_1.compatibility -->

<!-- UTSJSON.Array.find_1.tutorial -->

### find(predicate, thisArg?)

<!-- UTSJSON.Array.find_2.description -->

<!-- UTSJSON.Array.find_2.param -->

<!-- UTSJSON.Array.find_2.returnValue -->

<!-- UTSJSON.Array.find_2.test -->

<!-- UTSJSON.Array.find_2.compatibility -->

<!-- UTSJSON.Array.find_2.tutorial -->

D
DCloud_LXH 已提交
162 163 164 165 166 167 168 169
### findIndex(predicate, thisArg?)

<!-- UTSJSON.Array.findIndex.description -->

<!-- UTSJSON.Array.findIndex.param -->

<!-- UTSJSON.Array.findIndex.returnValue -->

M
mahaifeng 已提交
170
<!-- UTSJSON.Array.findIndex.test -->
D
DCloud_LXH 已提交
171 172 173

<!-- UTSJSON.Array.findIndex.compatibility -->

D
DCloud_LXH 已提交
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
### findIndex(predicate, thisArg?)

<!-- UTSJSON.Array.findIndex_1.description -->

<!-- UTSJSON.Array.findIndex_1.param -->

<!-- UTSJSON.Array.findIndex_1.returnValue -->

<!-- UTSJSON.Array.findIndex_1.test -->

<!-- UTSJSON.Array.findIndex_1.compatibility -->

<!-- UTSJSON.Array.findIndex_1.tutorial -->

### findIndex(predicate, thisArg?)

<!-- UTSJSON.Array.findIndex_2.description -->

<!-- UTSJSON.Array.findIndex_2.param -->

<!-- UTSJSON.Array.findIndex_2.returnValue -->

<!-- UTSJSON.Array.findIndex_2.test -->

<!-- UTSJSON.Array.findIndex_2.compatibility -->

<!-- UTSJSON.Array.findIndex_2.tutorial -->

D
DCloud_LXH 已提交
202 203 204 205 206 207 208 209
### fill(value, start?, end?)

<!-- UTSJSON.Array.fill.description -->

<!-- UTSJSON.Array.fill.param -->

<!-- UTSJSON.Array.fill.returnValue -->

M
mahaifeng 已提交
210
<!-- UTSJSON.Array.fill.test -->
D
DCloud_LXH 已提交
211

D
DCloud_LXH 已提交
212
需要注意的是,截止HBuilder 4.22  部分平台尚不支持[根据元素个数构造`Array`的写法](https://doc.dcloud.net.cn/uni-app-x/uts/buildin-object-api/array.html#constructor)
杜庆泉's avatar
杜庆泉 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

所以下面的代码在 部分平台可能不符合预期


```ts
new Array(20).fill(0)
```

可以使用下面的代码替代

```ts
let b = Array<Number>()
for(let i = 0; i < 20; i++){
  b.add(0)
}
```

D
DCloud_LXH 已提交
230 231 232 233 234 235 236 237 238 239
<!-- UTSJSON.Array.fill.compatibility -->

### copyWithin(target, start?, end?)

<!-- UTSJSON.Array.copyWithin.description -->

<!-- UTSJSON.Array.copyWithin.param -->

<!-- UTSJSON.Array.copyWithin.returnValue -->

M
mahaifeng 已提交
240
<!-- UTSJSON.Array.copyWithin.test -->
D
DCloud_LXH 已提交
241 242 243 244 245 246 247 248 249 250 251

<!-- UTSJSON.Array.copyWithin.compatibility -->

### pop()

<!-- UTSJSON.Array.pop.description -->

<!-- UTSJSON.Array.pop.param -->

<!-- UTSJSON.Array.pop.returnValue -->

M
mahaifeng 已提交
252
<!-- UTSJSON.Array.pop.test -->
D
DCloud_LXH 已提交
253 254 255

<!-- UTSJSON.Array.pop.compatibility -->

D
DCloud_LXH 已提交
256
### push(...items)
D
DCloud_LXH 已提交
257 258 259 260 261 262 263

<!-- UTSJSON.Array.push.description -->

<!-- UTSJSON.Array.push.param -->

<!-- UTSJSON.Array.push.returnValue -->

M
mahaifeng 已提交
264
<!-- UTSJSON.Array.push.test -->
D
DCloud_LXH 已提交
265 266 267

<!-- UTSJSON.Array.push.compatibility -->

D
DCloud_LXH 已提交
268
### concat(...items)
D
DCloud_LXH 已提交
269 270 271 272 273 274 275

<!-- UTSJSON.Array.concat.description -->

<!-- UTSJSON.Array.concat.param -->

<!-- UTSJSON.Array.concat.returnValue -->

M
mahaifeng 已提交
276
<!-- UTSJSON.Array.concat.test -->
D
DCloud_LXH 已提交
277 278 279

<!-- UTSJSON.Array.concat.compatibility -->

D
DCloud_LXH 已提交
280
### concat(...items)
D
DCloud_LXH 已提交
281 282 283 284 285 286 287

<!-- UTSJSON.Array.concat_1.description -->

<!-- UTSJSON.Array.concat_1.param -->

<!-- UTSJSON.Array.concat_1.returnValue -->

M
mahaifeng 已提交
288 289
<!-- UTSJSON.Array.concat_1.test -->

D
DCloud_LXH 已提交
290 291 292 293 294 295 296 297 298 299
<!-- UTSJSON.Array.concat_1.compatibility -->

### join(separator?)

<!-- UTSJSON.Array.join.description -->

<!-- UTSJSON.Array.join.param -->

<!-- UTSJSON.Array.join.returnValue -->

M
mahaifeng 已提交
300
<!-- UTSJSON.Array.concat_1.test -->
D
DCloud_LXH 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

<!-- UTSJSON.Array.join.compatibility -->

### reverse()

<!-- UTSJSON.Array.reverse.description -->

<!-- UTSJSON.Array.reverse.param -->

<!-- UTSJSON.Array.reverse.returnValue -->

<!-- UTSJSON.Array.reverse.compatibility -->

### shift()

<!-- UTSJSON.Array.shift.description -->

<!-- UTSJSON.Array.shift.param -->

<!-- UTSJSON.Array.shift.returnValue -->

M
mahaifeng 已提交
322
<!-- UTSJSON.Array.concat_1.test -->
D
DCloud_LXH 已提交
323 324 325 326 327 328 329 330 331 332 333

<!-- UTSJSON.Array.shift.compatibility -->

### slice(start?, end?)

<!-- UTSJSON.Array.slice.description -->

<!-- UTSJSON.Array.slice.param -->

<!-- UTSJSON.Array.slice.returnValue -->

M
mahaifeng 已提交
334
<!-- UTSJSON.Array.slice.test -->
D
DCloud_LXH 已提交
335 336 337 338 339 340 341 342 343 344 345

<!-- UTSJSON.Array.slice.compatibility -->

### sort(compareFn?)

<!-- UTSJSON.Array.sort.description -->

<!-- UTSJSON.Array.sort.param -->

<!-- UTSJSON.Array.sort.returnValue -->

M
mahaifeng 已提交
346
<!-- UTSJSON.Array.sort.test -->
D
DCloud_LXH 已提交
347 348 349

<!-- UTSJSON.Array.sort.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
**平台差异性**

在android平台,一定不能忽略两个对比元素相等的场景,否则可能会出现`java.lang.IllegalArgumentException: Comparison method violates its general contract!‌`
```

a.sort((a, b) : number => {
  // 这里的判断不能省略
  if(a.compareTo(b) == 0){
    return 0
  }
  return a - b
})
```



D
DCloud_LXH 已提交
366
### splice(start, deleteCount, ...items)
D
DCloud_LXH 已提交
367 368 369 370 371 372 373

<!-- UTSJSON.Array.splice.description -->

<!-- UTSJSON.Array.splice.param -->

<!-- UTSJSON.Array.splice.returnValue -->

M
mahaifeng 已提交
374
<!-- UTSJSON.Array.splice.test -->
D
DCloud_LXH 已提交
375 376 377

<!-- UTSJSON.Array.splice.compatibility -->

D
DCloud_LXH 已提交
378
### unshift(...items)
D
DCloud_LXH 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

<!-- UTSJSON.Array.unshift.description -->

<!-- UTSJSON.Array.unshift.param -->

<!-- UTSJSON.Array.unshift.returnValue -->

```ts
const array1 = [1, 2, 3];

console.log(array1.unshift(4, 5));
// expected output: 5

console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]
```

<!-- UTSJSON.Array.unshift.compatibility -->

### indexOf(searchElement, fromIndex?)

<!-- UTSJSON.Array.indexOf.description -->

<!-- UTSJSON.Array.indexOf.param -->

<!-- UTSJSON.Array.indexOf.returnValue -->

```ts
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beasts.indexOf('bison'));
// expected output: 1

// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4

console.log(beasts.indexOf('giraffe'));
// expected output: -1

```

<!-- UTSJSON.Array.indexOf.compatibility -->

### lastIndexOf(searchElement, fromIndex?)

<!-- UTSJSON.Array.lastIndexOf.description -->

<!-- UTSJSON.Array.lastIndexOf.param -->

<!-- UTSJSON.Array.lastIndexOf.returnValue -->

```ts
const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];

console.log(animals.lastIndexOf('Dodo'));
// expected output: 3

console.log(animals.lastIndexOf('Tiger'));
// expected output: 1
```

<!-- UTSJSON.Array.lastIndexOf.compatibility -->

### every(predicate, thisArg?)

<!-- UTSJSON.Array.every.description -->

<!-- UTSJSON.Array.every.param -->

<!-- UTSJSON.Array.every.returnValue -->

```ts
const isBelowThreshold = (currentValue:number):boolean => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// expected output: true
```

<!-- UTSJSON.Array.every.compatibility -->

D
DCloud_LXH 已提交
460
### every(predicate, thisArg?)
D
DCloud_LXH 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473

<!-- UTSJSON.Array.every_1.description -->

<!-- UTSJSON.Array.every_1.param -->

<!-- UTSJSON.Array.every_1.returnValue -->

<!-- UTSJSON.Array.every_1.test -->

<!-- UTSJSON.Array.every_1.compatibility -->

<!-- UTSJSON.Array.every_1.tutorial -->

D
DCloud_LXH 已提交
474
### every(predicate, thisArg?)
D
DCloud_LXH 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487

<!-- UTSJSON.Array.every_2.description -->

<!-- UTSJSON.Array.every_2.param -->

<!-- UTSJSON.Array.every_2.returnValue -->

<!-- UTSJSON.Array.every_2.test -->

<!-- UTSJSON.Array.every_2.compatibility -->

<!-- UTSJSON.Array.every_2.tutorial -->

D
DCloud_LXH 已提交
488
### every(predicate, thisArg?)
D
DCloud_LXH 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502

<!-- UTSJSON.Array.every_3.description -->

<!-- UTSJSON.Array.every_3.param -->

<!-- UTSJSON.Array.every_3.returnValue -->

<!-- UTSJSON.Array.every_3.test -->

<!-- UTSJSON.Array.every_3.compatibility -->

<!-- UTSJSON.Array.every_3.tutorial -->


D
DCloud_LXH 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
### some(predicate, thisArg?)

<!-- UTSJSON.Array.some.description -->

<!-- UTSJSON.Array.some.param -->

<!-- UTSJSON.Array.some.returnValue -->


```ts
const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element:number):boolean=> element % 2 == 0;

console.log(array.some(even));
// expected output: true
```

<!-- UTSJSON.Array.some.compatibility -->

D
DCloud_LXH 已提交
524
### some(predicate, thisArg?)
D
DCloud_LXH 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537

<!-- UTSJSON.Array.some_1.description -->

<!-- UTSJSON.Array.some_1.param -->

<!-- UTSJSON.Array.some_1.returnValue -->

<!-- UTSJSON.Array.some_1.test -->

<!-- UTSJSON.Array.some_1.compatibility -->

<!-- UTSJSON.Array.some_1.tutorial -->

D
DCloud_LXH 已提交
538
### some(predicate, thisArg?)
D
DCloud_LXH 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551

<!-- UTSJSON.Array.some_2.description -->

<!-- UTSJSON.Array.some_2.param -->

<!-- UTSJSON.Array.some_2.returnValue -->

<!-- UTSJSON.Array.some_2.test -->

<!-- UTSJSON.Array.some_2.compatibility -->

<!-- UTSJSON.Array.some_2.tutorial -->

D
DCloud_LXH 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
### forEach(callbackfn, thisArg?)

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

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

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

```ts
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a"
// expected output: "b"
// expected output: "c"
```

> 特别注意:
> 不可在 forEach 的 callbackFn 里添加或者删除原数组元素,此行为是危险的,在 Android 平台会造成闪退,在 iOS 平台会造成行为不符合预期。如果想实现该效果,请用 while 循环。

```ts

const array1 = ['a', 'b', 'c'];
array1.forEach(element => {
	console.log(element)
	array1.pop() // 此行为在 Android 平台会造成闪退,在 iOS 平台会输出 'a', 'b', 'c', 而 JS 会输出 'a', 'b'
});

// 如果想让上述行为正常运行,可以用 while 循环实现:

let array1 = ['a', 'b', 'c'];
let index = 0;
while (index < array1.length) {
  console.log(array1[index]);
  array1.pop();
  index += 1;
}

```

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

D
DCloud_LXH 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
### forEach(callbackfn, thisArg?)

<!-- UTSJSON.Array.forEach_1.description -->

<!-- UTSJSON.Array.forEach_1.param -->

<!-- UTSJSON.Array.forEach_1.returnValue -->

<!-- UTSJSON.Array.forEach_1.test -->

<!-- UTSJSON.Array.forEach_1.compatibility -->

<!-- UTSJSON.Array.forEach_1.tutorial -->

### forEach(callbackfn, thisArg?)

<!-- UTSJSON.Array.forEach_2.description -->

<!-- UTSJSON.Array.forEach_2.param -->

<!-- UTSJSON.Array.forEach_2.returnValue -->

<!-- UTSJSON.Array.forEach_2.test -->

<!-- UTSJSON.Array.forEach_2.compatibility -->

<!-- UTSJSON.Array.forEach_2.tutorial -->

D
DCloud_LXH 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
### map(callbackfn, thisArg?)

<!-- UTSJSON.Array.map.description -->

<!-- UTSJSON.Array.map.param -->

<!-- UTSJSON.Array.map.returnValue -->

```ts
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map((x:number):number => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

```

<!-- UTSJSON.Array.map.compatibility -->

D
DCloud_LXH 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
### map(callbackfn, thisArg?)

<!-- UTSJSON.Array.map_1.description -->

<!-- UTSJSON.Array.map_1.param -->

<!-- UTSJSON.Array.map_1.returnValue -->

<!-- UTSJSON.Array.map_1.test -->

<!-- UTSJSON.Array.map_1.compatibility -->

<!-- UTSJSON.Array.map_1.tutorial -->

### map(callbackfn, thisArg?)

<!-- UTSJSON.Array.map_2.description -->

<!-- UTSJSON.Array.map_2.param -->

<!-- UTSJSON.Array.map_2.returnValue -->

<!-- UTSJSON.Array.map_2.test -->

<!-- UTSJSON.Array.map_2.compatibility -->

<!-- UTSJSON.Array.map_2.tutorial -->

D
DCloud_LXH 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
### filter(predicate, thisArg?)

<!-- UTSJSON.Array.filter.description -->

<!-- UTSJSON.Array.filter.param -->

<!-- UTSJSON.Array.filter.returnValue -->

```ts
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter((word:string):boolean => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

```

<!-- UTSJSON.Array.filter.compatibility -->

D
DCloud_LXH 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
### filter(predicate, thisArg?)

<!-- UTSJSON.Array.filter_1.description -->

<!-- UTSJSON.Array.filter_1.param -->

<!-- UTSJSON.Array.filter_1.returnValue -->

<!-- UTSJSON.Array.filter_1.test -->

<!-- UTSJSON.Array.filter_1.compatibility -->

<!-- UTSJSON.Array.filter_1.tutorial -->

### filter(predicate, thisArg?)

<!-- UTSJSON.Array.filter_2.description -->

<!-- UTSJSON.Array.filter_2.param -->

<!-- UTSJSON.Array.filter_2.returnValue -->

<!-- UTSJSON.Array.filter_2.test -->

<!-- UTSJSON.Array.filter_2.compatibility -->

<!-- UTSJSON.Array.filter_2.tutorial -->

### filter(predicate, thisArg?)

<!-- UTSJSON.Array.filter_3.description -->

<!-- UTSJSON.Array.filter_3.param -->

<!-- UTSJSON.Array.filter_3.returnValue -->

<!-- UTSJSON.Array.filter_3.test -->

<!-- UTSJSON.Array.filter_3.compatibility -->

<!-- UTSJSON.Array.filter_3.tutorial -->

D
DCloud_LXH 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
### reduce(callbackfn)

<!-- UTSJSON.Array.reduce.description -->

<!-- UTSJSON.Array.reduce.param -->

<!-- UTSJSON.Array.reduce.returnValue -->

```ts
const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (previousValue:number, currentValue:number):number => previousValue + currentValue,
  initialValue
);

console.log(sumWithInitial);
// expected output: 10

```

<!-- UTSJSON.Array.reduce.compatibility -->

D
DCloud_LXH 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827

### reduce(callbackfn)

<!-- UTSJSON.Array.reduce_1.description -->

<!-- UTSJSON.Array.reduce_1.param -->

<!-- UTSJSON.Array.reduce_1.returnValue -->

<!-- UTSJSON.Array.reduce_1.test -->

<!-- UTSJSON.Array.reduce_1.compatibility -->

<!-- UTSJSON.Array.reduce_1.tutorial -->

### reduce(callbackfn, initialValue)

<!-- UTSJSON.Array.reduce_2.description -->

<!-- UTSJSON.Array.reduce_2.param -->

<!-- UTSJSON.Array.reduce_2.returnValue -->

<!-- UTSJSON.Array.reduce_2.test -->

<!-- UTSJSON.Array.reduce_2.compatibility -->

<!-- UTSJSON.Array.reduce_2.tutorial -->

### reduce(callbackfn, initialValue)

<!-- UTSJSON.Array.reduce_3.description -->

<!-- UTSJSON.Array.reduce_3.param -->

<!-- UTSJSON.Array.reduce_3.returnValue -->

<!-- UTSJSON.Array.reduce_3.test -->

<!-- UTSJSON.Array.reduce_3.compatibility -->

<!-- UTSJSON.Array.reduce_3.tutorial -->

### reduce(callbackfn, initialValue)

<!-- UTSJSON.Array.reduce_4.description -->

<!-- UTSJSON.Array.reduce_4.param -->

<!-- UTSJSON.Array.reduce_4.returnValue -->

<!-- UTSJSON.Array.reduce_4.test -->

<!-- UTSJSON.Array.reduce_4.compatibility -->

<!-- UTSJSON.Array.reduce_4.tutorial -->

### reduce(callbackfn, initialValue)

<!-- UTSJSON.Array.reduce_5.description -->

<!-- UTSJSON.Array.reduce_5.param -->

<!-- UTSJSON.Array.reduce_5.returnValue -->

<!-- UTSJSON.Array.reduce_5.test -->

<!-- UTSJSON.Array.reduce_5.compatibility -->

<!-- UTSJSON.Array.reduce_5.tutorial -->

D
DCloud_LXH 已提交
828 829 830 831 832 833 834 835 836 837
### reduceRight(callbackfn)

<!-- UTSJSON.Array.reduceRight.description -->

<!-- UTSJSON.Array.reduceRight.param -->

<!-- UTSJSON.Array.reduceRight.returnValue -->

<!-- UTSJSON.Array.reduceRight.compatibility -->

D
DCloud_LXH 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
### reduceRight(callbackfn)

<!-- UTSJSON.Array.reduceRight_1.description -->

<!-- UTSJSON.Array.reduceRight_1.param -->

<!-- UTSJSON.Array.reduceRight_1.returnValue -->

<!-- UTSJSON.Array.reduceRight_1.test -->

<!-- UTSJSON.Array.reduceRight_1.compatibility -->

<!-- UTSJSON.Array.reduceRight_1.tutorial -->

### reduceRight(callbackfn)

<!-- UTSJSON.Array.reduceRight_2.description -->

<!-- UTSJSON.Array.reduceRight_2.param -->

<!-- UTSJSON.Array.reduceRight_2.returnValue -->

<!-- UTSJSON.Array.reduceRight_2.test -->

<!-- UTSJSON.Array.reduceRight_2.compatibility -->

<!-- UTSJSON.Array.reduceRight_2.tutorial -->

### reduceRight(callbackfn, initialValue)

<!-- UTSJSON.Array.reduceRight_3.description -->

<!-- UTSJSON.Array.reduceRight_3.param -->

<!-- UTSJSON.Array.reduceRight_3.returnValue -->

<!-- UTSJSON.Array.reduceRight_3.test -->

<!-- UTSJSON.Array.reduceRight_3.compatibility -->

<!-- UTSJSON.Array.reduceRight_3.tutorial -->

### reduceRight(callbackfn, initialValue)

<!-- UTSJSON.Array.reduceRight_4.description -->

<!-- UTSJSON.Array.reduceRight_4.param -->

<!-- UTSJSON.Array.reduceRight_4.returnValue -->

<!-- UTSJSON.Array.reduceRight_4.test -->

<!-- UTSJSON.Array.reduceRight_4.compatibility -->

<!-- UTSJSON.Array.reduceRight_4.tutorial -->

### reduceRight(callbackfn, initialValue)

<!-- UTSJSON.Array.reduceRight_5.description -->

<!-- UTSJSON.Array.reduceRight_5.param -->

<!-- UTSJSON.Array.reduceRight_5.returnValue -->

<!-- UTSJSON.Array.reduceRight_5.test -->

<!-- UTSJSON.Array.reduceRight_5.compatibility -->

<!-- UTSJSON.Array.reduceRight_5.tutorial -->

### reduceRight(callbackfn, initialValue)

<!-- UTSJSON.Array.reduceRight_6.description -->

<!-- UTSJSON.Array.reduceRight_6.param -->

<!-- UTSJSON.Array.reduceRight_6.returnValue -->

<!-- UTSJSON.Array.reduceRight_6.test -->

<!-- UTSJSON.Array.reduceRight_6.compatibility -->

<!-- UTSJSON.Array.reduceRight_6.tutorial -->

D
DCloud_LXH 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
### isArray(arg)

<!-- UTSJSON.Array.isArray.description -->

<!-- UTSJSON.Array.isArray.param -->

<!-- UTSJSON.Array.isArray.returnValue -->

```ts
console.log(Array.isArray([1, 3, 5]));
// Expected output: true

console.log(Array.isArray('[]'));
// Expected output: false

console.log(Array.isArray(new Array(5)));
// Expected output: true

console.log(Array.isArray(new Int16Array([15, 33])));
// Expected output: false
```

<!-- UTSJSON.Array.isArray.compatibility -->

### includes(searchElement, fromIndex?)

<!-- UTSJSON.Array.includes.description -->

<!-- UTSJSON.Array.includes.param -->

<!-- UTSJSON.Array.includes.returnValue -->

<!-- UTSJSON.Array.includes.compatibility -->

### toKotlinList()

<!-- UTSJSON.Array.toKotlinList.description -->

<!-- UTSJSON.Array.toKotlinList.param -->

<!-- UTSJSON.Array.toKotlinList.returnValue -->

<!-- UTSJSON.Array.toKotlinList.compatibility -->

<!-- UTSJSON.Array.toKotlinList.tutorial -->

<!-- UTSJSON.Array.tutorial -->

杜庆泉's avatar
杜庆泉 已提交
970 971


杜庆泉's avatar
杜庆泉 已提交
972
## Android 平台方法
杜庆泉's avatar
杜庆泉 已提交
973

杜庆泉's avatar
杜庆泉 已提交
974
* 目前 Array 类型编译到 `kotlin``io.dcloud.uts.UTSArray`, 该类继承自 `java.util.ArrayList`,所有`java` /`kotlin` 为其提供的扩展函数(如:`toTypedArray` 等),均可以正常调用。
杜庆泉's avatar
杜庆泉 已提交
975

杜庆泉's avatar
杜庆泉 已提交
976

977
::: preview
杜庆泉's avatar
杜庆泉 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

> UTS

```ts
let utsArray = ["1",2,3.0]
// UTSArray 分别转换为 Java Array / Kotlin Array
let javaArray = utsArray.toTypedArray();
let kotlinArray = utsArray.toKotlinList()
// 从Java Array 转换为 UTSArray
let convertArrayFromJava = Array.fromNative(javaArray);
// 从Kotlin Array 转换为 UTSArray
let convertArrayFromKotlin = Array.fromNative(kotlinArray);
```

> Kotlin

```kotlin
val utsArray = utsArrayOf("1",2,3.0)
// UTSArray 分别转换为 Java Array / Kotlin Array
val javaArray = utsArray.toTypedArray();
val kotlinArray = utsArray.toKotlinList()
// 从Java Array 转换为 UTSArray
val convertArrayFromJava = UTSArray.fromNative(javaArray);
// 从Kotlin Array 转换为 UTSArray
val convertArrayFromKotlin = UTSArray.fromNative(kotlinArray);
```

:::


杜庆泉's avatar
杜庆泉 已提交
1008
更多平台专属Array 参考[文档](https://doc.dcloud.net.cn/uni-app-x/uts/data-type.html#kotlin%E4%B8%93%E6%9C%89%E6%95%B0%E7%BB%84%E7%B1%BB%E5%9E%8B)
杜庆泉's avatar
杜庆泉 已提交
1009

D
DCloud_LXH 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
## 常见操作

- 创建数组
```ts
const fruits = ['Apple', 'Banana']
console.log(fruits.length)
```
- 通过索引访问数组元素
```ts
const first = fruits[0]
// Apple
const last = fruits[fruits.length - 1]
// Banana
```
- 遍历数组
```ts
fruits.forEach(function(item, index, array) {
  console.log(item, index)
})
// Apple 0
// Banana 1
```
- 注意:数组遍历不推荐使用 for in 语句,因为在 ts 中 for in 遍历的是数组的下标,而在 Swift 和 Kottlin 中遍历的是数组的元素,存在行为不一致。

- 添加元素到数组的末尾
```ts
const newLength = fruits.push('Orange')
// ["Apple", "Banana", "Orange"]
```
- 删除数组末尾的元素
```ts
const last = fruits.pop() // remove Orange (from the end)
// ["Apple", "Banana"]
```
- 删除数组头部元素
```ts
const first = fruits.shift() // remove Apple from the front
// ["Banana"]
```
- 添加元素到数组的头部
```ts
const newLength = fruits.unshift('Strawberry') // add to the front
// ["Strawberry", "Banana"]
```
- 找出某个元素在数组中的索引
```ts
fruits.push('Mango')
// ["Strawberry", "Banana", "Mango"]
const pos = fruits.indexOf('Banana')
// 1
```
- 通过索引删除某个元素
```ts
const removedItem = fruits.splice(pos, 1) // this is how to remove an item
// ["Strawberry", "Mango"]
```
- 从一个索引位置删除多个元素
```ts
const vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
console.log(vegetables)
// ["Cabbage", "Turnip", "Radish", "Carrot"]
const pos = 1
const n = 2
const removedItems = vegetables.splice(pos, n)
// this is how to remove items, n defines the number of items to be removed,
// starting at the index position specified by pos and progressing toward the end of array.
console.log(vegetables)
// ["Cabbage", "Carrot"] (the original array is changed)
console.log(removedItems)
// ["Turnip", "Radish"]
```
- 复制一个数组
```ts
const shallowCopy = fruits.slice() // this is how to make a copy
// ["Strawberry", "Mango"]
```
### 访问数组元素

数组的索引是从 0 开始的,第一个元素的索引为 0,最后一个元素的索引等于该数组的 长度 减 1。

如果指定的索引是一个无效值,将会抛出 IndexOutOfBoundsException 异常

下面的写法是错误的,运行时会抛出 SyntaxError 异常,而原因则是使用了非法的属性名:

```ts
console.log(arr.0) // a syntax error
```