gpt4 book ai didi

javascript - 如何获取最后 5 个元素,不包括数组中的第一个元素?

转载 作者:IT老高 更新时间:2023-10-28 13:17:12 26 4
gpt4 key购买 nike

在 JavaScript 数组中,如何获取最后 5 个元素,不包括第一个元素

[1, 55, 77, 88] // ...would return [55, 77, 88]

添加更多示例:

[1, 55, 77, 88, 99, 22, 33, 44] // ...would return [88, 99, 22, 33, 44]

[1] // ...would return []

最佳答案

您可以调用:

arr.slice(Math.max(arr.length - 5, 1))

如果不想排除第一个元素,请使用

arr.slice(Math.max(arr.length - 5, 0))

关于javascript - 如何获取最后 5 个元素,不包括数组中的第一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6473858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com