这篇文章主要介绍“TypeScript数组Array操作的常用方法有哪些”,在日常操作中,相信很多人在TypeScript数组Array操作的常用方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”TypeScript数组Array操作的常用方法有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
let array1:Array<number>;
let array2:number[];
let array1:Array<number> = new Array<number>();
let array2:number[] = [1,2,3];
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
/**
* 连接数组
*/
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
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]
到此,关于“TypeScript数组Array操作的常用方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。