获取数组的最后两个位置 [英] Get the last two positions of an array

查看:127
本文介绍了获取数组的最后两个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何像这样获得该数组的最后两个元素:

I would like to know how can I obtain the last two elements of this array like this:

array = [1,5,7,8,10,12,23,24];

我正在尝试使用slice() function进行获取,但是它对我而言不起作用,因为我一直想获得最后两个职位.

I am trying to obtain with slice() function but it doesn´t work for me, beacuse I always I want to obtain the last two positions.

array = [23,24];

我希望任何人都可以在这个问题上为我提供帮助.

I hope that anyone can help me in this question.

致谢!

推荐答案

使用具有负索引的数组#slice .

var array = [1,5,7,8,10,12,23,24];

console.log(array.slice(-2));

您还可以这里.

这篇关于获取数组的最后两个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆