获取数组最后 n 个元素的推荐方法是使用 slice(-n) 或 _.takeright();1. 使用 array.prototype.slice(-n) 可直接获取末尾 n 个元素,若 n 大于数组长度则返回整个数组,若 n 为 0 或负数则返回空数组(但 slice(-0) 等同于 slice(0),返回整个数组);2. 使用 lodash 的 _.takeright(array, n) 语义更清晰,行为更符合直觉,n 为 0 或负数时明确返回空数组;选择取决于是否已引入 lodash 及对代码可读性的要求,原生 slice 无需依赖且性能佳,而 takeright 更利于表达意图且处理边界更一致。

在 JavaScript 中,如果你想获取一个数组的最后
n
Array.prototype.slice()
_.takeRight()
要从 JavaScript 数组中获取末尾的
n
1. 使用原生 JavaScript 的 Array.prototype.slice()
这是最常见且无需额外依赖的方式。
slice()
const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const n = 3; // 想要获取的最后3个元素 // 获取数组的最后 n 个元素 const lastNElements = myArray.slice(-n); console.log(lastNElements); // 输出: [8, 9, 10] // 如果 n 等于或大于数组长度,它会返回整个数组的副本 const allElements = myArray.slice(-15); // n=15,但数组只有10个元素 console.log(allElements); // 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // 如果 n 为 0 或负数,会返回空数组 const emptyResult = myArray.slice(0); // slice(0) 返回整个数组,但 slice(-0) 返回空数组 console.log(emptyResult); // 输出: [] (slice(-0) 的结果) const anotherEmpty = myArray.slice(-Infinity); // slice(-Infinity) 返回整个数组 console.log(anotherEmpty); // 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
这里需要澄清一下
slice(-0)
slice(0)
slice(-0)
0
-0
slice(0, 0)
slice(anyIndex, anyIndex)
更正: 对于
slice(-0)
slice(0)
slice
-0
n
[]
array.slice(array.length, array.length)
// 重新演示 n 为 0 的情况,如果要获取0个元素,通常是直接返回空数组或者不进行操作。 // slice(-0) 的行为和 slice(0) 相同,都会返回整个数组的副本。 const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const nZero = 0; const lastZeroElements = myArray.slice(-nZero); // 实际上是 slice(-0),等同于 slice(0) console.log(lastZeroElements); // 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // 如果明确需要空数组,且 n 为 0,可以这样处理: const resultForZeroN = (nZero === 0) ? [] : myArray.slice(-nZero); console.log(resultForZeroN); // 输出: []
2. 使用 Lodash 库的 _.takeRight()
如果你项目中已经引入了 Lodash,
_.takeRight()
n
import _ from 'lodash'; // 或者 const _ = require('lodash');
const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const n = 3;
// 获取数组的最后 n 个元素
const lastNElementsLodash = _.takeRight(myArray, n);
console.log(lastNElementsLodash); // 输出: [8, 9, 10]
// 行为与 slice 类似,如果 n 大于数组长度,返回整个数组
const allElementsLodash = _.takeRight(myArray, 15);
console.log(allElementsLodash); // 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// 如果 n 为 0 或负数,返回空数组
const emptyResultLodash = _.takeRight(myArray, 0);
console.log(emptyResultLodash); // 输出: []
const anotherEmptyLodash = _.takeRight(myArray, -5);
console.log(anotherEmptyLodash); // 输出: []slice(-n)
_.takeRight()
我常常思考,当我们需要数组末尾的元素时,是直接用原生
slice
takeRight
takeRight
slice(-n)
slice(-n)
n
slice(-n)
takeRight
_.takeRight()
n
slice
n=0
_.takeRight()
takeRight
takeRight
takeRight
slice(-n)
一个很典型的场景是日志或消息流展示。想象一个聊天应用,你只想显示最新的 10 条消息,或者一个系统日志面板,你只想看到最近的 20 条操作记录。这时候,日志数组或者消息数组通常是按时间顺序递增的,那么获取
takeRight(logArray, 10)
再比如分页功能,特别是那种“加载更多”或者“无限滚动”的场景。虽然多数分页是取前面 N 条,但有时候也会有取最后 N 条的逻辑,比如在一个时间轴上展示最近的事件。
还有就是数据预览或摘要。如果有一个非常大的数据集,你可能只想给用户展示最后几条数据作为预览,比如“最近上传的 5 张图片”或者“最新加入的 3 位用户”。这都是
takeRight
在一些状态管理的场景,比如实现一个简单的撤销(undo)功能,你可能维护一个操作历史栈。当用户点击撤销时,你需要获取最近一次操作,并将其从栈中移除。虽然
pop()
takeRight
takeRight
当然,除了
slice(-n)
_.takeRight()
一个常见的替代方案是结合 Array.prototype.length
Array.prototype.slice()
array.length - n
const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const n = 3; const startIndex = Math.max(0, myArray.length - n); // 确保起始索引不会是负数 const lastElementsAlt = myArray.slice(startIndex); console.log(lastElementsAlt); // 输出: [8, 9, 10]
这种方式虽然稍微复杂一点,但它的好处是明确地使用正数索引,对于不熟悉
slice
n
Math.max(0, ...)
另一个值得一提但通常不推荐用于“获取”的,是 Array.prototype.splice()
splice()
splice
const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const n = 3; // 警告:splice 会修改原数组! // const removedElements = myArray.splice(myArray.length - n, n); // console.log(removedElements); // 输出: [8, 9, 10] // console.log(myArray); // 输出: [1, 2, 3, 4, 5, 6, 7] - 原数组被修改了
所以,除非你的目的就是从数组末尾“剪掉”并获取这些元素,否则不要用
splice
对于仅仅需要获取最后一个元素的情况,
Array.prototype.pop()
const myArray = [1, 2, 3, 4, 5]; const lastElement = myArray.pop(); // 5 console.log(lastElement); console.log(myArray); // [1, 2, 3, 4]
如果你只需要获取最后一个元素而不修改数组,那么
myArray[myArray.length - 1]
在极少数情况下,为了极致的性能优化(比如处理千万级别的数据,且不希望引入额外函数调用栈),可能会考虑手动循环。但这在现代 JavaScript 开发中已经非常少见了,因为
slice
takeRight
总的来说,当你需要获取数组尾部元素时,
slice(-n)
_.takeRight()
以上就是js 如何用takeRight获取数组的后n个元素的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号