Common methods for TypeScript array Array operations

This article mainly introduces the common methods of TypeScript array Array operation. The article introduces it in great detail through example code. It has certain reference learning value for everyone's study or work. Friends who need it can follow the editor to learn together.

Table of contents

Array is a very simple data structure, but every time I use an array in TypeScript, I always forget how to use it. I just make it simple and forget to come and take a look.

1. Array declaration

1

2

let array1:Array<number>;

let array2:number[];

2. Array initialization

1

2

let array1:Array<number> = new Array<number>();

let array2:number[] = [1,2,3];

3. Assignment, addition, and change of array elements

1

2

3

4

5

6

7

8

9

10

11

12

13

14

let array:Array<number> = [1,2,3,4];

console.log(array)      // [1, 2, 3, 4]

array[0] = 20;          // 修改

console.log(array)      // [20, 2, 3, 4]

array[4] = 5;           // 赋值

console.log(array)      // [20, 2, 3, 4, 5]

array.push(6);          // 添加

console.log(array)      // [20, 2, 3, 4, 5, 6]

array.unshift(8, 0);    // 在第一个位置依次添加

console.log(array);     // [8, 0, 20, 2, 3, 4, 5, 6]

4. Delete

1

2

3

4

5

6

7

8

9

10

11

let array:Array<number> = [1,2,3,4];

console.log(array)      // [1, 2, 3, 4]

let popValue = array.pop();     // 弹出

console.log(array)      // [1, 2, 3]

array.splice(0, 1);     // 删除元素(index, deleteCount)

console.log(array)      // [2, 3]

array.shift();          // 删除第一个元素

console.log(array);     // [3]

5. Merge and disconnect arrays

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

/**

  * Combines two or more arrays.

  * @param items Additional items to add to the end of array1.

  */

concat(...items: T[][]): T[];

/**

  * Combines two or more arrays.

  * @param items Additional items to add to the end of array1.

  */

concat(...items: (T | T[])[]): T[];

/**

 * 该方法返回指定起始位置的一个新的数组

 */

slice(start?: number, end?: number): T[];

let array: Array<number> = [1, 2, 3];

let array2: Array<number> = [4, 5, 6];

let arrayValue = 7;

array = array.concat( array2);

console.log(array)          // [1, 2, 3, 4, 5, 6]

array = array.concat(arrayValue);

console.log(array)          // [1, 2, 3, 4, 5, 6, 7]

let newArray = array.slice(2, 4);

console.log(newArray)      // [3, 4]

6. Find the position of array elements

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/**

  * 返回查找到的第一个元素所在位置

  */

indexOf(searchElement: T, fromIndex?: number): number;

/**

  * 返回反序查找的第一个元素所在位置

  */

lastIndexOf(searchElement: T, fromIndex?: number): number;

let array: Array<string> = ["a","b","c","d","c","a"];

let indexC = array.indexOf("c");

console.log(indexC);            // 2

let lastA = array.lastIndexOf("a");

console.log(lastA);             // 5

7. Connect array elements

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

 * 连接数组

 */

join(separator?: string): string;

let array: Array<string> = ["a","b","c","d","c","a"];

let result = array.join();

console.log(result);            // a,b,c,d,c,a

result = array.join("+");

console.log(result);            // a+b+c+d+c+a

result = array.join("");

console.log(result);            // abcdca

8. Sort and reverse arrays

1

2

3

4

5

6

7

8

let array:Array<number> = [3, 2, 1, 8, 7, 0, 4];

console.log(array);             // [3, 2, 1, 8, 7, 0, 4]

array.sort();

console.log(array);             // [0, 1, 2, 3, 4, 7, 8]

array.reverse();

console.log(array);             // [8, 7, 4, 3, 2, 1, 0]

9. Please see here for traversal

TypeScript traversal Array method (for, forEach, every) - CSDN Blog

Original text: Common methods for TypeScript array Array operations_javascript skills_Script Home

Guess you like

Origin blog.csdn.net/weixin_42565127/article/details/134008152