TypeScript中的数组操作

发布时间:2021-05-06

介绍

我们在编码时,总会用到数组/列表这种类型,用于在单个对象中存储多个内容。在 TypeScript 中,也已经内置了该类型,方便我们来使用。

操作

初始化

使用下面的代码即可创建并初始化一个数组类型的对象。其中,在构造函数中不输入参数即表示创建一个空数组。

let array : string[] = ["1", "2", "3"];

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

let anyArray : Array<any> = new Array<any>();

修改

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]

删除

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]

合并、断开数组

/**
  * 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]

查找数组元素位置

/**
  * 返回查找到的第一个元素所在位置
  */
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

连接数组元素

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

排序、反序

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]
其他阅读

网页小技巧

分享一些网页开发中实用的UI小技巧,快速完成页面搭建工作。

查看原文

WPF打包成单文件

在开发WPF程序时,有时我们需要把整个软件打包成一个文件,这样可以方便分发,本文将会介绍怎么把WPF打包成单文件形式。

查看原文

C#中new和override的区别

在C#编程语言中,new 和 override 是两个重要的关键字,它们用于控制类成员方法的行为。在面向对象编程(OOP)中,理解这两个关键字的区别和用法,对于编写清晰、可维护和高效的代码至关重要。

查看原文

Angular使用路由复用实现单页多窗(Tab页形式)

我们在开发后台管理系统时,一个很重要的需求就是多窗口编辑,来回切换使用。一种思路是使用iframe来记录多个多页面展示,本文通过Angular路由复用来实现这一需求。

查看原文

Top 命令输出

top 命令是 Linux 中一个很常用的命令,其作用是将系统的性能统计和进程运行情况输出到控制台中,本文带着大家解析 top 命令输出的内容具体作用。

查看原文