获取数组中的最后一项 [英] Get the last item in an array

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

问题描述

以下是我的JavaScript代码:

Here is my JavaScript code so far:

var linkElement = document.getElementById("BackButton");
var loc_array = document.location.href.split('/');
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2]))); 
linkElement.appendChild(newT);

目前,它从URL中获取数组中的倒数第二项。但是,我想检查数组中的最后一项是index.html,如果是这样,请抓住第三项到最后一项。

Currently it takes the second to last item in the array from the URL. However I want to do a check for the last item in the array to be "index.html" and if so, grab the third to last item instead.

推荐答案

使用 Array.pop

var lastItem = anArray.pop();

重要:这会返回最后一个元素 从数组中移除

Important : This returns the last element and removes it from the array

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

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