1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- //string
- let studentName: String = "xiaoming";
- //number
- let studentAge: number = 18;
- //Boolean
- let isChecked: Boolean = true;
- //Date
- let now: Date = new Date();
- //定义接口
- interface StudentInt {
- name: string;
- age: number;
- isChecked: boolean;
- works?: Array<string>;
- }
- //定义函数接口
- interface CheckCountFunction {
- (studentList: Array<StudentInt>): number;
- }
- //Array
- let studentList1: Array<{ name: string; age: number; isChecked: boolean }> = [
- { name: "xiaoming", age: 18, isChecked: true },
- { name: "xiaowang'", age: 18, isChecked: true },
- ];
- let studentnew: StudentInt = {
- name: "xiaoming",
- age: 18,
- isChecked: false,
- works: ["yuwen"],
- };
- studentList1.push(studentnew);
- /**接口:实现提示词检索
- * @param progress {number} 任务进度
- * @param complete {boolean} 任务是否完成
- * @param errorMsg {string} 错误信息
- */
- interface AgentTask {
- progress: number; //任务进度
- complete: boolean; //任务是否完成
- errorMsg: string; //错误信息
- }
- let task1: AgentTask = { progress: 1, complete: true, errorMsg: "youwenti" };
- /**
- * 统计人数 没有接口表示
- * @param studentList{Array<StudentInt} 学生列表
- * @returns
- */
- // let checkList = studentList.map(item=>item.isChecked ? 1:0)
- // let count = 0;
- // checkList.forEach(check=>{
- // if(check==1)
- // count ++
- // })
- // let checkList = studentList.map((item): 1 | 0 => (item.isChecked ? 1 : 0));
- // let count = 0;
- // checkList.forEach((checked) => {
- // if (checked == 1) count++;
- // });
- // 使用reduce方法直接累加isChecked为true的学生数量
- function checkCount1(studentList: Array<StudentInt>): number {
- return studentList.reduce((count, student) => {
- return count + (student.isChecked ? 1 : 0);
- }, 0);
- }
- /**
- * 统计人数 用函数接口表示
- * @param studentList{Array<StudentInt} 学生列表
- * @returns
- */
- const checkCount: CheckCountFunction = (
- studentList: Array<StudentInt>
- ): number => {
- let checkList = studentList.map((item): 1 | 0 => (item.isChecked ? 1 : 0));
- let count = 0;
- checkList.forEach((checked) => {
- if (checked === 1) count++;
- });
- return count;
- };
- let count = checkCount(studentList1);
- console.log(count);
|