获取 Javascript 数组中的最后一项

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);

答案

if(loc_array[loc_array.length-1] == 'index.html'){
 //do something
}else{
 //something else.
}
arr.slice(-1)[0]
arr.slice(-1).pop()
var lastItem = anArray.pop();
Array.prototype.last = function() {
    return this[this.length - 1];
}
export function last(array) {
    return array[array.length - 1];
}
var last = arr[arr.length - 1]
var last = arr.slice(-1)[0]
a = [1,2,5,6,1,874,98,"abc"];
a.length; //returns 8 elements
a.pop();  // will return "abc" AND REMOVES IT from the array 
a.length; // returns 7
a.slice(-1).pop(); // will return "abc" won't do modify the array 
                   // because slice creates a new array object 
a.length;          // returns 8; no modification and you've got you last element
const foo = [1,2,3,4];
const bar = [...foo].pop();
while(loc_array.pop()!= "index.html"){
}
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length])));
arr.slice(-1)[0]
var fileName = loc_array.slice(-1)[0];
if(fileName.toLowerCase() == "index.html")
{
  //your code...
}