//創建數組
var array01: number[] = [1, 2, 3, 4, 5];
var array02: Array<any> = [1, 2, 2, 3, "string"];
var array03: Array<number> = new Array(1, 2, 3, 4);
//感覺元組就是一種r特殊的數組
var variable: [string, string] = ["1", ""];
variable.push("true");
console.log(variable);
variable.shift();
console.log(variable);
//試試數組的一些方法
for (var each in array01) {
console.log(each);
}
console.log(array01.concat(array02));
function checkBiggerThan4(element: number, index: any, array: any) {
console.log(`now is checking ${element} and result is ${element < 4}`);
return (element as number) < 4;
}
//返回true,相當於continue;返回false,相當於break;
array01.every(checkBiggerThan4);
array01.forEach((each, index, array) => console.log(`第${index}個元素是${each}`));
var result: string = "**";
console.log(array02.join(result));
console.log(array01.map(
(each) => {
//一定要有返回值!返回值會作爲map出來的新值放進一個新數組
return each * 2 + 1;
}
)
);
var ouuputResult: any =
array01.reduce((previous, current, currentIndex, outputArray) => {
console.log(`${previous}+${current}=${previous + current}`);
return previous + current;
}
);
console.log(ouuputResult);
/*
PS C:\CRroot\documents\codeproject\tampermonkey-scriptps> tsc .\typescript_數組.ts;node .\typescript_數組.js
[ '1', '', 'true' ]
[ '', 'true' ]
0
1
2
3
4
[ 1, 2, 3, 4, 5, 1, 2, 2, 3, 'string' ]
now is checking 1 and result is true
now is checking 2 and result is true
now is checking 3 and result is true
now is checking 4 and result is false
第0個元素是1
第1個元素是2
第2個元素是3
第3個元素是4
第4個元素是5
1**2**2**3**string
[ 3, 5, 7, 9, 11 ]
1+2=3
3+3=6
6+4=10
10+5=15
15
*/
typescript 數組部分代碼實踐:every()、map()、foreach()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.