ts文件運(yùn)行環(huán)境
- 安裝 TypeScript
npm install -g typescript
- 安裝 ts-node
npm install -g ts-node
- 運(yùn)行ts文件,cmd下
ts-node demo.ts
一、數(shù)組的聲明
let arr1: Array<number>;
let arr2: number[];
二、數(shù)組初始化
let arr1: Array<number> = new Array<number>();
let arr2: number[] = [1,2,3];
三、數(shù)組元素賦值、添加、更改
// 基本變量不需要聲明后可以不需要初始化;
let a:number;
a = 1;
console.log(a); // 1
let b: string;
b = "string";
console.log(b); // string
// 數(shù)組和對象使用之前必須要初始化;
let arr1: Array<number>;
// arr1[0] = 100; // Variable 'arr1' is used before being assigned.
//arr1.push(100); // 就算使用push來添加也不行
//console.log(arr1)
let arr2: Array<number> = new Array<number>();
arr2[0] = 1; // 賦值和修改格式一樣
arr2.push(2); // 最后面增加,可以多個值
arr2.unshift(3) // 最前面增加,可以多個值
console.log(arr2) // [3, 1, 2, 3]
四、刪除
let arr: Array<number> = [1,2,3,4,5]
arr.pop();
console.log(arr); // [1, 2, 3, 4]
arr.shift();
console.log(arr); // [2, 3, 4]
arr.splice(0,2); // 刪除元素(index, deleteCount)
console.log(arr); // [4]
五、合并、斷開數(shù)組
let arr: Array<number> = [1,2,3]
let arr2: Array<number> = [4,5,6]
let arrValue = 7
arr = arr.concat(arr2)
console.log(arr) //[1, 2, 3, 4, 5, 6]
arr = arr.concat(arrValue)
console.log(arr) //[1, 2, 3, 4, 5, 6, 7]
let newArray = arr.slice(1, 3)
console.log(newArray) // [2,3]
六、查找數(shù)組元素
let arr: Array<string> = ["a", "b", "c", "d"]
let index = arr.indexOf("c") //返回查找到的第一個元素所在位置
console.log(index) // 2
index = arr.lastIndexOf("d") //返回反序查找的第一個元素所在位置
console.log(index) // 3
// 對象
let persons = [
{ id: 1, name: '張三', age: 23 },
{ id: 2, name: '李四', age: 11 },
{ id: 3, name: '王五', age: 16 }
];
// persons.forEach((value, index, array) => {
// console.log(value, index)
// })
// find() 方法返回相應(yīng)的對象, 從未返回真值,則 find() 方法返回 undefined
const person = persons.find(obj => {
return obj.id === 2;
})
console.log(person); // { "id": 2, "name": "李四", "age": 11}
// filter, 也可以反向選擇
const p1 = persons.filter(obj => {
// return obj.name != "李四"
return obj.id == 2;
})
console.log(p1); // { "id": 2, "name": "李四", "age": 11}
const p2 = persons.filter(obj => {
return obj.id != 2;
})
console.log(p2);
/**
[{
"id": 1,
"name": "張三",
"age": 23
}, {
"id": 3,
"name": "王五",
"age": 16
}]
*/
// 查找元素的index
const c1 = persons.findIndex(person => {
return person.name == "李四"
})
console.log(c1); // 1
console.log(persons[c1]) //{ "id": 2, "name": "李四", "age": 11}
七、連接數(shù)組元素
let arr: Array<string> = ["a", "b", "c", "d"]
let joinString = arr.join(",") //返回查找到的第一個元素所在位置
console.log(joinString) // "a,b,c,d"
joinString = arr.join("-") //返回反序查找的第一個元素所在位置
console.log(joinString) // "a-b-c-d"
八、排序、反序數(shù)組
let arr: Array<number> = [1,4,3,5,2]
arr.sort()
console.log(arr) // [1, 2, 3, 4, 5]
arr.reverse() //返回反序查找的第一個元素所在位置
console.log(arr) // [5, 4, 3, 2, 1]
九、遍歷數(shù)組,對象
- 定義數(shù)組
let persons = [
{ id: 1, name: '張三', age: 23 },
{ id: 2, name: '李四', age: 11 },
{ id: 3, name: '王五', age: 16 }
];
- for 循環(huán)
for ( let index = 0; index < persons.length; ++index) {
console.log(persons[index]);
}
- for…in 循環(huán)
for (let index in persons) {
console.log(persons[index])
}
- for…of循環(huán)
for (let person of persons) {
console.log(person)
}
- forEach循環(huán)
persons.forEach((value, index, array) => {
console.log(value, index)
})
十、刪除數(shù)組,對象中符合條件的數(shù)據(jù)
-
目前要上面的遍歷方法都有問題,因為 刪除后,索引就變了
-
目前能想到和實(shí)現(xiàn)的辦法是通過
while
來實(shí)現(xiàn)
let persons = [
{ id: 1, name: '張三', age: 23 },
{ id: 2, name: '李四', age: 11 },
{ id: 3, name: '王五', age: 16 },
{ id: 4, name: '王xx', age: 16 }
];
let i = persons.length;
while (i--) {
if (persons[i].id == 1 || persons[i].id == 3) {
persons.splice(i, 1);
}
}
console.log(persons);
/**
[{
"id": 2,
"name": "李四",
"age": 11
}, {
"id": 4,
"name": "王xx",
"age": 16
}]
*/
-
刪除在另一個數(shù)組中包含的元素:文章來源:http://www.zghlxwxcb.cn/news/detail-683291.html
-
刪除在另一個數(shù)組中不包含的元素:文章來源地址http://www.zghlxwxcb.cn/news/detail-683291.html
deleteNotInList(currList: any[], otherList: any[]) {
let i = currList.length
while (i--) {
if (!otherList.some((x) => {
return currList[i].scanItemName === x.scanItemName
})) currList.splice(i, 1)
}
}
到了這里,關(guān)于TypeScript數(shù)組和對象的操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!