
本文介绍了如何使用 TypeScript 从数组中提取最后 N 个元素。我们将讨论在数组长度满足特定条件时返回原数组,否则返回数组的后 N 个元素的方法,并提供代码示例和改进方案,帮助开发者更灵活地处理数组数据。
问题分析
原始代码存在一些问题:
- 逻辑错误: 使用 || (OR) 运算符判断数组长度是否在 2 到 10 之间是不正确的。应该使用 && (AND) 运算符来确保数组长度同时大于等于 2 且小于等于 10。
- 数组创建: 代码中创建了一个新的数组 result,并将传入的 name 和 schedule 包装成一个对象后放入数组。这导致函数始终返回一个包含一个元素的数组,而不是操作传入的数组。
- 切片数量错误: 原始代码中 slice(-9) 试图返回倒数第 9 个元素到最后一个元素,而题目要求返回最后 10 个元素。
解决方案
以下是修正后的代码,它接受一个数组作为输入,并根据数组长度返回不同的结果:
interface ScheduleItem {
course: string;
hours: number;
}
interface Schedule {
name: string;
schedule: ScheduleItem[];
}
export const sliceData = (dataToSlice: Schedule[], fromLastElement: number = 10): Schedule[] => {
if (dataToSlice.length >= 2 && dataToSlice.length <= 10) {
return dataToSlice; // 返回原始数组
}
// 否则,返回数组的最后 N 个元素
return dataToSlice.slice(-fromLastElement);
}代码解释:
- 接口定义: 定义了 ScheduleItem 和 Schedule 接口,用于类型注解,提高代码可读性和可维护性。
-
函数签名: sliceData 函数接受两个参数:
- dataToSlice: 要处理的 Schedule 类型的数组。
- fromLastElement: 可选参数,表示要返回的最后元素的数量,默认为 10。
- 条件判断: 使用 dataToSlice.length >= 2 && dataToSlice.length
- 数组切片: 如果数组长度不在 2 到 10 之间,则使用 dataToSlice.slice(-fromLastElement) 返回数组的最后 fromLastElement 个元素。slice() 方法的负数索引表示从数组末尾开始计数。
使用示例
const myArr: Schedule[] = [
{ "name": "John", "schedule": [{ "course": "ESL", "hours": 25 }, { "course": "Math", "hours": 50 }, { "course": "History", "hours": 75 }] },
{ "name": "Julia", "schedule": [{ "course": "English", "hours": 20 }, { "course": "Geography", "hours": 35 }, { "course": "Math", "hours": 55 }] },
{ "name": "Adam", "schedule": [{ "course": "Physics", "hours": 15 }, { "course": "Math", "hours": 50 }, { "course": "Chemistry", "hours": 60 }] }
];
const slicedArray = sliceData(myArr); // 返回 myArr (因为长度小于等于 10)
console.log(slicedArray);
const longArray: Schedule[] = [
{ "name": "John", "schedule": [{ "course": "ESL", "hours": 25 }, { "course": "Math", "hours": 50 }, { "course": "History", "hours": 75 }] },
{ "name": "Julia", "schedule": [{ "course": "English", "hours": 20 }, { "course": "Geography", "hours": 35 }, { "course": "Math", "hours": 55 }] },
{ "name": "Adam", "schedule": [{ "course": "Physics", "hours": 15 }, { "course": "Math", "hours": 50 }, { "course": "Chemistry", "hours": 60 }] },
{ "name": "John", "schedule": [{ "course": "ESL", "hours": 25 }, { "course": "Math", "hours": 50 }, { "course": "History", "hours": 75 }] },
{ "name": "Julia", "schedule": [{ "course": "English", "hours": 20 }, { "course": "Geography", "hours": 35 }, { "course": "Math", "hours": 55 }] },
{ "name": "Adam", "schedule": [{ "course": "Physics", "hours": 15 }, { "course": "Math", "hours": 50 }, { "course": "Chemistry", "hours": 60 }] },
{ "name": "John", "schedule": [{ "course": "ESL", "hours": 25 }, { "course": "Math", "hours": 50 }, { "course": "History", "hours": 75 }] },
{ "name": "Julia", "schedule": [{ "course": "English", "hours": 20 }, { "course": "Geography", "hours": 35 }, { "course": "Math", "hours": 55 }] },
{ "name": "Adam", "schedule": [{ "course": "Physics", "hours": 15 }, { "course": "Math", "hours": 50 }, { "course": "Chemistry", "hours": 60 }] },
{ "name": "John", "schedule": [{ "course": "ESL", "hours": 25 }, { "course": "Math", "hours": 50 }, { "course": "History", "hours": 75 }] },
{ "name": "Julia", "schedule": [{ "course": "English", "hours": 20 }, { "course": "Geography", "hours": 35 }, { "course": "Math", "hours": 55 }] },
{ "name": "Adam", "schedule": [{ "course": "Physics", "hours": 15 }, { "course": "Math", "hours": 50 }, { "course": "Chemistry", "hours": 60 }] }
];
const slicedLongArray = sliceData(longArray); // 返回 longArray 的最后 10 个元素
console.log(slicedLongArray);
const slicedLongArrayCustom = sliceData(longArray, 5); // 返回 longArray 的最后 5 个元素
console.log(slicedLongArrayCustom);总结
本文提供了一个 TypeScript 函数,用于从数组中提取最后 N 个元素。通过修正原始代码中的逻辑错误,并添加了类型注解和可选参数,使函数更加健壮、灵活和易于使用。 开发者可以根据实际需求调整 fromLastElement 参数,以获取所需数量的最后元素。










