TS 从 0 到 1 - 数组和对象

2023-05-17 19:43:23 浏览数 (1)

# 数组

# 数组解构

代码语言:javascript复制
let x: number;
let y: number;
let z: number;

let five_array = [0, 1, 2, 3, 4];
[x, y, z] = five_array;
console.log(x, y, z); // 0 1 2

# 数组展开运算符

代码语言:javascript复制
let two_array = [0, 1];
let five_array = [...two_array, 2, 3, 4];
console.log(five_array); // [0, 1, 2, 3, 4]

# 数组遍历

代码语言:javascript复制
let colors: string[] = ["red", "green", "blue"];

for (let color of colors) {
  console.log(color);
}

# 对象

# 对象解构

代码语言:javascript复制
let person = {
  name: "Cell",
  gender: 'Male',
};

let { name, gender } = person;

# 对象展开运算符

代码语言:javascript复制
let person = {
  name: "Cell",
  gender: "Male",
  address: "Earth",
};

// 组装对象
let person2 = { ...person, age: 18 };

// 获取除了某些项外的其他项
let { name, ...rest } = person;

0 人点赞