
本文介绍了如何使用 TypeScript 从数组中提取最后 N 个元素。我们将讨论在数组长度满足特定条件时返回原数组,否则返回数组的后 N 个元素的方法,并提供代码示例和改进方案,帮助开发者更灵活地处理数组数据。
原始代码存在一些问题:
以下是修正后的代码,它接受一个数组作为输入,并根据数组长度返回不同的结果:
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);
}代码解释:
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 参数,以获取所需数量的最后元素。
以上就是从 TypeScript 数组中获取最后 N 个元素的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号