In a javascript array, how do I get the last 5 elements, excluding the first element?

前端 未结 6 1944
自闭症患者
自闭症患者 2020-11-30 20:19
[1, 55, 77, 88] // ...would return [55, 77, 88]

adding additional examples:

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


        
6条回答
  •  悲&欢浪女
    2020-11-30 20:28

    You can call:

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

    If you don't want to exclude the first element, use

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

提交回复
热议问题