TypeScript 數(shù)組Array操作的常用方法
更新時間:2022年06月21日 16:00:55 作者:honey199396
本文主要介紹了TypeScript 數(shù)組Array操作的常用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
數(shù)組是一個很簡單的數(shù)據(jù)結(jié)構(gòu),但是每次使用TypeScript的數(shù)組的時候又總是忘記怎么用了,干脆直接弄成干貨,忘了過來看看。
一、數(shù)組的聲明
let array1:Array<number>; let array2:number[];
二、數(shù)組初始化
let array1:Array<number> = new Array<number>(); let array2:number[] = [1,2,3];
三、數(shù)組元素賦值、添加、更改
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]
五、合并、斷開數(shù)組
/** ? * 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[]; /** ?* 該方法返回指定起始位置的一個新的數(shù)組 ?*/ 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]
六、查找數(shù)組元素位置
/**
? * 返回查找到的第一個元素所在位置
? */
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七、連接數(shù)組元素
/**
?* 連接數(shù)組
?*/
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八、排序、反序數(shù)組
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]
九、遍歷請看這里
到此這篇關(guān)于TypeScript 數(shù)組Array操作的常用方法的文章就介紹到這了,更多相關(guān)TypeScript 數(shù)組Array操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- TypeScript數(shù)組的定義與使用詳解
- typeScript中數(shù)組類型定義及應(yīng)用詳解
- TypeScript編寫自動創(chuàng)建長度固定數(shù)組的類型工具詳解
- TypeScript實現(xiàn)數(shù)組和樹的相互轉(zhuǎn)換
- TypeScript調(diào)整數(shù)組元素順序算法
- TypeScript中Array(數(shù)組)聲明與簡單使用方法
- TypeScript之元組、數(shù)組及as?const的使用
- TypeScript判斷兩個數(shù)組的內(nèi)容是否相等的實現(xiàn)
- TypeScript數(shù)組實現(xiàn)棧與對象實現(xiàn)棧的區(qū)別詳解
- TypeScript之元組、數(shù)組、多維數(shù)組定義方法以及 as const說明
相關(guān)文章
微信小程序?qū)崿F(xiàn)語音識別轉(zhuǎn)文字功能及遇到的坑
這篇文章主要介紹了小程序?qū)崿F(xiàn)語音識別轉(zhuǎn)文字功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
js apply/call/caller/callee/bind使用方法與區(qū)別分析
js apply/call/caller/callee/bind使用方法與區(qū)別分析,需要的朋友可以參考下。2009-10-10
d3.js實現(xiàn)簡單的網(wǎng)絡(luò)拓?fù)鋱D實例代碼
最近一直在學(xué)習(xí)d3.js,大家都知道d3.js是一個非常不錯的數(shù)據(jù)可視化庫,我們可以用它來做一些比較酷的東西,比如可以來顯示一些簡單的網(wǎng)絡(luò)拓?fù)鋱D,這篇文中就通過實例代碼給大家介紹了如何利用d3.js實現(xiàn)簡單的網(wǎng)絡(luò)拓?fù)鋱D,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11

